逻辑与证明

Logic and Proof

1.介绍

1. Introduction

1.1.数学证明

1.1. Mathematical Proof

尽管有书面证据表明早在公元前 3000 年埃及就出现了数学活动,但许多学者认为真正的数学诞生于公元前六世纪左右的古希腊,当时首次引入了演绎证明。亚里士多德认为米利都的泰勒斯不仅认识到了我们所知道的东西的重要性,而且认识到了我们如何知道的重要性,并在演绎方法中找到了知识的基础。公元前 300 年左右,欧几里得在他的论文《几何原本》中编纂了几何学的演绎方法。几个世纪以来,欧几里得的公理化风格被视为严格论证的典范,不仅在数学中,而且在哲学和科学中也是如此。

Although there is written evidence of mathematical activity in Egypt as early as 3000 BC, many scholars locate the birth of mathematics proper in ancient Greece around the sixth century BC, when deductive proof was first introduced. Aristotle credited Thales of Miletus with recognizing the importance of not just what we know but how we know it, and finding grounds for knowledge in the deductive method. Around 300 BC, Euclid codified a deductive approach to geometry in his treatise, the Elements. Through the centuries, Euclid’s axiomatic style was held as a paradigm of rigorous argumentation, not just in mathematics, but in philosophy and the sciences as well.

这是一个用当代数学语言来表达的普通证明的例子。它证实了毕达哥拉斯学派所知道的一个事实。

Here is an example of an ordinary proof, in contemporary mathematical language. It establishes a fact that was known to the Pythagoreans.


定理。 2是无理数,也就是说,它不能用分数来表示a/b, 在哪里ab是整数。

Theorem. 2 is irrational, which is to say, it cannot be expressed as a fraction a/b, where a and b are integers.

证明。假设2=a/b对于某对整数ab。通过消除所有共同因素,我们可以假设a/b是最低的,因此ab没有共同的因素。那么我们有a=2b,然后对两边取平方,可得a2=2b2

Proof. Suppose 2=a/b for some pair of integers a and b. By removing any common factors, we can assume a/b is in lowest terms, so that a and b have no factor in common. Then we have a=2b, and squaring both sides, we have a2=2b2.

最后一个等式意味着a2是偶数,而奇数的平方也是奇数,a本身也必定是偶数。因此我们有a=2c对于某个整数c. 将其代入方程a2=2b2,我们有4c2=2b2,因此2c2=b2.这意味着b2是偶数,所以b也同样如此。

The last equation implies that a2 is even, and since the square of an odd number is odd, a itself must be even as well. We therefore have a=2c for some integer c. Substituting this into the equation a2=2b2, we have 4c2=2b2, and hence 2c2=b2. This means that b2 is even, and so b is even as well.

事实是ab甚至与以下事实相矛盾:ab没有共同因素。因此最初的假设2=a/b是错误的。

The fact that a and b are both even contradicts the fact that a and b have no common factor. So the original assumption that 2=a/b is false.


在下一个例子中,我们关注自然数,

In the next example, we focus on the natural numbers,

N={0,1,2,}.

自然数n如果大于或等于 2,则称其为合数,如果它可以写成乘积n=mk既不m也不k等于1,否则为素数。注意如果n=mk见证了这一事实n是复合的,那么mk都小于n还要注意,按照惯例,0 和 1 既不被视为质数也不被视为合数。

A natural number n greater than or equal to 2 is said to be composite if it can be written as a product n=mk where neither m nor k is equal to 1, and prime otherwise. Notice that if n=mk witnesses the fact that n is composite, then m and k are both smaller than n. Notice also that, by convention, 0 and 1 are considered neither prime nor composite.


定理。每个大于或等于 2 的自然数都可以写成素数的乘积。

Theorem. Every natural number greater than or equal to 2 can be written as a product of primes.

证明。我们通过归纳法进行n。 让n为大于 2 的任意自然数。如果n是素数,我们就完成了;我们可以考虑n本身就是一个项的乘积。否则,n是复合的,我们可以写成n=mk在哪里mk小于n并且大于 1。根据归纳假设,mk可以写成素数的乘积,比如 m=p1p2puk=q1q2qv.但随后我们有

Proof. We proceed by induction on n. Let n be any natural number greater than 2. If n is prime, we are done; we can consider n itself as a product with one term. Otherwise, n is composite, and we can write n=mk where m and k are smaller than n and greater than 1. By the inductive hypothesis, each of m and k can be written as a product of primes, say m=p1p2pu and k=q1q2qv. But then we have

n=mk=p1p2puq1q2qv,

根据需要,素数的乘积。

a product of primes, as required.


稍后,我们将看到更多的事实是真实的:每一个大于 2 的自然数都可以用独特的方式写成素数的乘积,这一事实被称为算术基本定理

Later, we will see that more is true: every natural number greater than 2 can be written as a product of primes in a unique way, a fact known as the fundamental theorem of arithmetic.

本课程的首要目标是教您编写清晰易读的数学证明。我们将通过考虑大量示例来实现这一目标,同时也采取反思的观点:我们将仔细研究数学语言的组成部分和数学证明的结构,以便更好地理解它们的工作原理。

The first goal of this course is to teach you to write clear, readable mathematical proofs. We will do this by considering a number of examples, but also by taking a reflective point of view: we will carefully study the components of mathematical language and the structure of mathematical proofs, in order to gain a better understanding of how they work.

1.2.符号逻辑

1.2. Symbolic Logic

为了理解证明的工作原理,研究“符号逻辑”这一主题将大有裨益,它提供了数学语言和证明的理想模型。在《前分析篇》中,这位古希腊哲学家着手分析推理模式,并发展了三段论理论。以下是三段论的一个例子:

Toward understanding how proofs work, it will be helpful to study a subject known as “symbolic logic,” which provides an idealized model of mathematical language and proof. In the Prior Analytics, the ancient Greek philosopher set out to analyze patterns of reasoning, and developed the theory of the syllogism. Here is one instance of a syllogism:


每个人都是动物。

Every man is an animal.

任何动物都会死。

Every animal is mortal.

因此,每个人都终有一死。

Therefore every man is mortal.


亚里士多德指出,这种推论的正确性与个别陈述的真实性或虚假性无关,而是与一般模式有关:

Aristotle observed that the correctness of this inference has nothing to do with the truth or falsity of the individual statements, but, rather, the general pattern:


每个 A 都是 B。

Every A is B.

每个 B 都是 C。

Every B is C.

因此每个 A 都是 C。

Therefore every A is C.


我们可以用各种属性来代替 A、B 和 C;试着用鱼、独角兽、游泳生物、神话生物等属性来代替。由此产生的各种陈述可能为真或为假,但所有实例都具有以下关键特征:如果两个假设为真,则结论也为真。我们通过说推理有效来表达这一点

We can substitute various properties for A, B, and C; try substituting the properties of being a fish, being a unicorn, being a swimming creature, being a mythical creature, etc. The various statements that result may come out true or false, but all the instantiations will have the following crucial feature: if the two hypotheses come out true, then the conclusion comes out true as well. We express this by saying that the inference is valid.

尽管亚里士多德的推理理论所针对的语言模式有限,但我们仍要感谢他提出的一个关键洞见:我们可以根据逻辑形式对有效的推理模式进行分类,同时抽象出具体内容。这一基本观察是整个符号逻辑领域的基础。

Although the patterns of language addressed by Aristotle’s theory of reasoning are limited, we have him to thank for a crucial insight: we can classify valid patterns of inference by their logical form, while abstracting away specific content. It is this fundamental observation that underlies the entire field of symbolic logic.

17 世纪,莱布尼茨提出了“普遍性”的设计,这是一种通用的符号语言,人们可以用它精确地表达任何断言,以及“推理演算”,这是一种“思维演算”,可以表达精确的推理规则。莱布尼茨自己也采取了一些措施来开发这种语言和演算,但更大的进步是在 19 世纪取得的,这要归功于布尔、弗雷格、皮尔斯、施罗德等人的努力。20 世纪初,这些努力在数理逻辑领域蓬勃发展。

In the seventeenth century, Leibniz proposed the design of a characteristica universalis, a universal symbolic language in which one would express any assertion in a precise way, and a calculus ratiocinatur, a “calculus of thought” which would express the precise rules of reasoning. Leibniz himself took some steps to develop such a language and calculus, but much greater strides were made in the nineteenth century, through the work of Boole, Frege, Peirce, Schroeder, and others. Early in the twentieth century, these efforts blossomed into the field of mathematical logic.

如果您考虑上一节中的证明示例,您会注意到一些术语和推理规则特定于当前主题,与数字以及质数、合数、偶数、奇数等属性有关。但还有其他术语和推理规则并非特定于领域,例如与“每个”、“一些”、“和”和“如果……那么”等词相关的术语和规则。符号逻辑的目标是识别这些推理和论证的核心要素并解释它们的工作原理,以及解释如何引入和使用更多特定领域的概念。

If you consider the examples of proofs in the last section, you will notice that some terms and rules of inference are specific to the subject matter at hand, having to do with numbers and the properties of being prime, composite, even, odd, and so on. But there are other terms and rules of inference that are not domain specific, such as those related to the words “every,” “some,” “and,” and “if … then.” The goal of symbolic logic is to identify these core elements of reasoning and argumentation and explain how they work, as well as to explain how more domain-specific notions are introduced and used.

为此,我们将引入关键逻辑概念的符号,包括以下内容:

To that end, we will introduce symbols for key logical notions, including the following:

  • AB,“if A then B

  • AB, “if A then B

  • AB,“A and B

  • AB, “A and B

  • AB,“A or B

  • AB, “A or B

  • ¬A,“not A

  • ¬A, “not A

  • xA,“for every xA

  • xA, “for every xA

  • xA,“for some xA

  • xA, “for some xA

然后,我们将提供一个正式的证明系统,让我们通过演绎的方式证明这些陈述之间的某些蕴涵是有效的。

We will then provide a formal proof system that will let us establish, deductively, that certain entailments between such statements are valid.

我们将使用的证明系统是自然演绎法的一种,这是 Gerhard Gentzen 在 20 世纪 30 年代引入的一种证明系统,用于模拟非正式论证风格。在这个系统中,判断的基本单位是断言一个陈述,A,根据一组有限的假设,Γ. 写为ΓA。 如果ΓΔ是两个有限的假设集,我们将写成Γ,Δ表示这两个集合的并集,即由每个集合中的所有假设组成的集合。根据这些约定,合取符号的规则可以表示如下:

The proof system we will use is a version of natural deduction, a type of proof system introduced by Gerhard Gentzen in the 1930s to model informal styles of argument. In this system, the fundamental unit of judgment is the assertion that a statement, A, follows from a finite set of hypotheses, Γ. This is written as ΓA. If Γ and Δ are two finite sets of hypotheses, we will write Γ,Δ for the union of these two sets, that is, the set consisting of all the hypotheses in each. With these conventions, the rule for the conjunction symbol can be expressed as follows:

这应该被理解为:假设A根据假设Γ, 和B根据假设ΔAB根据这两个假设ΓΔ

This should be interpreted as saying: assuming A follows from the hypotheses Γ, and B follows from the hypotheses Δ, AB follows from the hypotheses in both Γ and Δ.

我们将看到,人们可以更紧凑地写出这样的证明,而隐含假设,这样上面的规则就可以表达如下:

We will see that one can write such proofs more compactly leaving the hypotheses implicit, so that the rule above is expressed as follows:

在这种格式中,上一节中第一个证明的片段可能呈现如下:

In this format, a snippet of the first proof in the previous section might be rendered as follows:

此类证明的复杂性很快就会变得无法控制,即使是基本数学事实的完整证明也会变得相当冗长。此类系统并非为编写严肃的数学而设计。相反,它们提供了理想化的数学推理模型,只要它们捕捉到某种非正式证明的结构,它们就使我们能够研究数学推理的性质。

The complexity of such proofs can quickly grow out of hand, and complete proofs of even elementary mathematical facts can become quite long. Such systems are not designed for writing serious mathematics. Rather, they provide idealized models of mathematical inference, and insofar as they capture something of the structure of an informal proof, they enable us to study the properties of mathematical reasoning.

本课程的第二个目标是帮助您理解自然演绎,作为正式演绎系统的一个示例。

The second goal of this course is to help you understand natural deduction, as an example of a formal deductive system.

1.3.交互式定理证明

1.3. Interactive Theorem Proving

数理逻辑的早期研究旨在表明,至少在原则上,普通数学论证可以用符号演算建模。如上所述,复杂性问题限制了实践中可以实现的范围;即使是基本的数学论证也需要很长的推导过程,这些推导过程很难写,也很难读,而且对促进对底层数学的理解几乎没有帮助。

Early work in mathematical logic aimed to show that ordinary mathematical arguments could be modeled in symbolic calculi, at least in principle. As noted above, complexity issues limit the range of what can be accomplished in practice; even elementary mathematical arguments require long derivations that are hard to write and hard to read, and do little to promote understanding of the underlying mathematics.

然而,自 20 世纪末以来,计算证明助手的出现开始使完全形式化成为可能。通过与定理证明软件交互,用户可以构建复杂定理的形式化推导,这些推导可以由计算机存储和检查。自动化方法可用于手动填补小空白、公理化地验证长计算或确定性地填充长推理链。然而,自动化的范围目前相当有限。交互式定理证明中使用的策略是要求用户提供足够的信息,以便系统能够构建和检查形式化推导。这通常涉及用一种为此目的而设计的“编程语言”编写证明。例如,这是Lean定理证明器中的一个简短证明:

Since the end of the twentieth century, however, the advent of computational proof assistants has begun to make complete formalization feasible. Working interactively with theorem proving software, users can construct formal derivations of complex theorems that can be stored and checked by computer. Automated methods can be used to fill in small gaps by hand, verify long calculations axiomatically, or fill in long chains of inferences deterministically. The reach of automation is currently fairly limited, however. The strategy used in interactive theorem proving is to ask users to provide just enough information for the system to be able to construct and check a formal derivation. This typically involves writing proofs in a sort of “programming language” that is designed with that purpose in mind. For example, here is a short proof in the Lean theorem prover:

section
variables (P Q : Prop)

theorem my_theorem : P  Q  Q  P :=
assume h : P  Q,
have P, from and.left h,
have Q, from and.right h,
show Q  P, from and.intro Q P

end
section
variables (P Q : Prop)

theorem my_theorem : P  Q  Q  P :=
assume h : P  Q,
have P, from and.left h,
have Q, from and.right h,
show Q  P, from and.intro Q P

end

如果您以在线形式阅读当前文本,您会在正式的“证明脚本”上方看到一个按钮,上面写着“试试看!”按下该按钮会在编辑器窗口中打开证明,并在浏览器中运行 Lean 版本来处理证明,将其转换为公理推导,并验证其正确性。您可以通过改变编辑器中的文本进行实验;任何错误都会在右侧窗口中注明。

If you are reading the present text in online form, you will find a button above the formal “proof script” that says “try it!” Pressing the button opens the proof in an editor window and runs a version of Lean inside your browser to process the proof, turn it into an axiomatic derivation, and verify its correctness. You can experiment by varying the text in the editor; any errors will be noted in the window to the right.

Lean 中的证明可以访问先前的数学结果库,所有结果都经过公理基础验证。交互式定理证明领域的一个目标是达到任何当代定理都可以通过这种方式验证的程度。例如,下面是一个正式证明,证明二的平方根是无理数,遵循上面提出的非正式证明的模型:

Proofs in Lean can access a library of prior mathematical results, all verified down to axiomatic foundations. A goal of the field of interactive theorem proving is to reach the point where any contemporary theorem can be verified in this way. For example, here is a formal proof that the square root of two is irrational, following the model of the informal proof presented above:

import data.nat.prime
open nat

theorem sqrt_two_irrational {a b : } (co : gcd a b = 1) :
  a^2  2 * b^2 :=
assume h : a^2 = 2 * b^2,
have 2  a^2,
  by simp [h],
have 2  a,
  from prime.dvd_of_dvd_pow prime_two this,
exists.elim this $
assume (c : nat) (aeq : a = 2 * c),
have 2 * (2 * c^2) = 2 * b^2,
  by simp [eq.symm h, aeq];
    simp [pow_succ', mul_comm, mul_assoc, mul_left_comm],
have 2 * c^2 = b^2,
  from mul_left_cancel' dec_trivial this,
have 2  b^2,
  by simp [eq.symm this],
have 2  b,
  from prime.dvd_of_dvd_pow prime_two this,
have 2  gcd a b,
  from dvd_gcd 2  a 2  b›,
have 2  (1 : ),
  by simp * at *,
show false, from absurd 2  1 dec_trivial
import data.nat.prime
open nat

theorem sqrt_two_irrational {a b : } (co : gcd a b = 1) :
  a^2  2 * b^2 :=
assume h : a^2 = 2 * b^2,
have 2  a^2,
  by simp [h],
have 2  a,
  from prime.dvd_of_dvd_pow prime_two this,
exists.elim this $
assume (c : nat) (aeq : a = 2 * c),
have 2 * (2 * c^2) = 2 * b^2,
  by simp [eq.symm h, aeq];
    simp [pow_succ', mul_comm, mul_assoc, mul_left_comm],
have 2 * c^2 = b^2,
  from mul_left_cancel' dec_trivial this,
have 2  b^2,
  by simp [eq.symm this],
have 2  b,
  from prime.dvd_of_dvd_pow prime_two this,
have 2  gcd a b,
  from dvd_gcd 2  a 2  b›,
have 2  (1 : ),
  by simp * at *,
show false, from absurd 2  1 dec_trivial

本课程的第三个目标是教您用精益语言编写基本证明。我们要求您在精益语言中证明的事实将比我们要求您编写的非正式证明更基本,但我们的目的是正式证明将模拟和阐明我们将教给您的非正式证明策略。

The third goal of this course is to teach you to write elementary proofs in Lean. The facts that we will ask you to prove in Lean will be more elementary than the informal proofs we will ask you to write, but our intent is that formal proofs will model and clarify the informal proof strategies we will teach you.

1.4.语义观点

1.4. The Semantic Point of View

正如我们在此介绍的主题,符号逻辑的目标是指定一种语言和推理规则,使我们能够以可靠的方式获得真相。这个想法是,我们选择的符号表示具有固定含义的对象和概念,我们采用的推理规则使我们能够从真实的假设中得出真实的结论。

As we have presented the subject here, the goal of symbolic logic is to specify a language and rules of inference that enable us to get at the truth in a reliable way. The idea is that the symbols we choose denote objects and concepts that have a fixed meaning, and the rules of inference we adopt enable us to draw true conclusions from true hypotheses.

然而,人们可以采用另一种观点来看待逻辑,即逻辑是一个系统,其中某些符号具有固定的含义,例如“与”、“或”和“非”的符号,而其他符号的含义可以变化。例如,表达式P(QR), 读 ”P并且要么Q或者R”可能为真也可能为假,这取决于以下基本断言 PQ R 代表。更准确地说,复合表达式的真值仅取决于组成符号表示的表达式是真还是假。例如,如果PQ, 和R分别代表“七是质数”、“七是偶数”和“七是奇数”,则表达式为真。如果我们用“六”代替“七”,则该语句为假。更一般地,只要P是真的,并且至少有一个QR为真,否则为假。

One can adopt another view of logic, however, as a system where some symbols have a fixed meaning, such as the symbols for “and,” “or,” and “not,” and others have a meaning that is taken to vary. For example, the expression P(QR), read “P and either Q or R,” may be true or false depending on the basic assertions that P, Q, and R stand for. More precisely, the truth of the compound expression depends only on whether the component symbols denote expressions that are true or false. For example, if P, Q, and R stand for “seven is prime,” “seven is even,” and “seven is odd,” respectively, then the expression is true. If we replace “seven” by “six,” the statement is false. More generally, the expression comes out true whenever P is true and at least one of Q and R is true, and false otherwise.

从这个角度来看,逻辑与其说是一种断言真理的语言,不如说是一种描述可能事态的语言。换句话说,逻辑提供了一种规范语言,表达式可以是真或假,这取决于我们如何解释允许变化的符号。例如,如果我们固定了基本谓词的含义,那么“两个蓝色块之间有一个红色块”这个陈述在给定的块“世界”中可能是真或假,我们可以用这个表达式来描述它为真的世界集。这种逻辑观在计算机科学中很重要,我们使用逻辑表达式从数据库中选择符合某些标准的条目,指定硬件和软件系统的属性,或者断言我们希望约束求解器满足的约束。

From this perspective, logic is not so much a language for asserting truth, but a language for describing possible states of affairs. In other words, logic provides a specification language, with expressions that can be true or false depending on how we interpret the symbols that are allowed to vary. For example, if we fix the meaning of the basic predicates, the statement “there is a red block between two blue blocks” may be true or false of a given “world” of blocks, and we can take the expression to describe the set of worlds in which it is true. Such a view of logic is important in computer science, where we use logical expressions to select entries from a database matching certain criteria, to specify properties of hardware and software systems, or to assert constraints that we would like a constraint solver to satisfy.

一方面,句法 / 演绎观点与另一方面的语义 / 模型理论观点之间存在重要联系。我们将在此过程中探索其中的一些联系。例如,我们将看到,可以将“有效”断言视为在所有可能的非固定符号解释下都为真的断言,将“有效”推论视为在所有可能的状态和事务中都保持真实的推论。从这个角度来看,演绎系统应该只允许我们推导出有效的断言和蕴涵,这种属性称为健全性。如果演绎系统足够强大,可以让我们验证所有有效的断言和蕴涵,则称其为完备的

There are important connections between the syntactic / deductive point of view on the one hand, and the semantic / model-theoretic point of view on the other. We will explore some of these along the way. For example, we will see that it is possible to view the “valid” assertions as those that are true under all possible interpretations of the non-fixed symbols, and the “valid” inferences as those that maintain truth in all possible states and affairs. From this point of view, a deductive system should only allow us to derive valid assertions and entailments, a property known as soundness. If a deductive system is strong enough to allow us to verify all valid assertions and entailments, it is said to be complete.

本课程的第四个目标是传达逻辑的语义观点,并引导您了解如何使用逻辑表达式来指定事态。

The fourth goal of this course is to convey the semantic view of logic, and to lead you to understand how logical expressions can be used to specify states of affairs.

1.5.目标总结

1.5. Goals Summarized

总而言之,本课程的目标是:

To summarize, these are the goals of this course:

  • 您应该学会写出清晰的、“有文化的”数学证明。

  • You should learn to write clear, “literate,” mathematical proofs.

  • 您应该熟悉符号逻辑和演绎证明的形式建模。

  • You should become comfortable with symbolic logic and the formal modeling of deductive proof.

  • 您应该学习如何使用交互式校对助手。

  • You should learn how to use an interactive proof assistant.

  • 您应该了解如何使用逻辑作为一种精确的语言来对对象系统及其之间的关系提出主张,并指定某些事态。

  • You should understand how to use logic as a precise language for making claims about systems of objects and the relationships between them, and specifying certain states of affairs.

让我们花点时间考虑一下这些目标之间的关系。重要的是不要混淆前三个目标。我们处理的是三种数学语言:普通数学语言、数学逻辑的符号表示和交互式证明助手中的计算实现。这些都是非常不同的东西!

Let us take a moment to consider the relationship between some of these goals. It is important not to confuse the first three. We are dealing with three kinds of mathematical language: ordinary mathematical language, the symbolic representations of mathematical logic, and computational implementations in interactive proof assistants. These are very different things!

符号逻辑并非旨在取代普通数学语言,你不应该使用如下符号在普通的数学证明中,符号表达式的作用并不比在写给父母的信中用符号表达式代替“and”和“or”要大。自然语言提供了表达的细微差别,可以传达超越模式匹配验证正确性的多层次含义和理解。同时,用符号表达式对数学语言进行建模提供了一定程度的精确度,使得将数学语言本身变成研究对象成为可能。每种表达式都有其适用之处,我们希望让您在不混淆两者的情况下欣赏每种表达式的价值。

Symbolic logic is not meant to replace ordinary mathematical language, and you should not use symbols like and in ordinary mathematical proofs any more than you would use them in place of the words “and” and “or” in letters home to your parents. Natural languages provide nuances of expression that can convey levels of meaning and understanding that go beyond pattern matching to verify correctness. At the same time, modeling mathematical language with symbolic expressions provides a level of precision that makes it possible to turn mathematical language itself into an object of study. Each has its place, and we hope to get you to appreciate the value of each without confusing the two.

交互式定理证明器使用的证明语言介于两个极端之间。一方面,它们必须具有足够的精度,以便计算机能够处理它们并做出适当的反应;另一方面,它们旨在捕捉非正式语言的一些高级细微差别和特征,以便我们能够编写更复杂的论证和证明。它们植根于符号逻辑,并在设计时考虑到了普通数学语言,旨在弥合两者之间的差距。

The proof languages used by interactive theorem provers lie somewhere between the two extremes. On the one hand, they have to be specified with enough precision for a computer to process them and act appropriately; on the other hand, they aim to capture some of the higher-level nuances and features of informal language in a way that enables us to write more complex arguments and proofs. Rooted in symbolic logic and designed with ordinary mathematical language in mind, they aim to bridge the gap between the two.

本书还旨在向您展示数学是如何从基本概念构建起来的。逻辑提供了游戏规则,然后我们从集合、关系、函数和自然数的性质逐步发展到初等数论、组合学和实数的性质。最后一章以对公理基础的讨论结束了这个故事。

This book also aims to show you how mathematics is built up from fundamental concepts. Logic provides the rules of the game, and then we work our way up from properties of sets, relations, functions, and the natural numbers to elementary number theory, combinatorics, and properties of the real numbers. The last chapter rounds out the story with a discussion of axiomatic foundations.

1.6.关于本教材

1.6. About this Textbook

这本在线教科书和Lean定理证明器都是新项目,正在进行中。您可以从其项目页面、Leann社区页面和在线教科书《Lean 中的定理证明》了解有关 Lean 的更多信息。

Both this online textbook and the Lean theorem prover are new and ongoing projects. You can learn more about Lean from its project page, the Leann community pages, and the online textbook, Theorem Proving in Lean.

我们非常感谢许多人的反馈和更正,包括 Bruno Cuconato、William DeMeo、Tobias Grosser、Lyle Kopnicky、Alexandre Rademaker、Matt Rice 和 Jason Siefken。

We are grateful for feedback and corrections from a number of people, including Bruno Cuconato, William DeMeo, Tobias Grosser, Lyle Kopnicky, Alexandre Rademaker, Matt Rice, and Jason Siefken.

2.命题逻辑

2. Propositional Logic

2.1.谜题

2.1. A Puzzle

下面的谜题名为“恶意与爱丽丝”,出自乔治·J·萨默斯的《逻辑推理谜题》。

The following puzzle, titled “Malice and Alice,” is from George J. Summers’ Logical Deduction Puzzles.


爱丽丝、爱丽丝的丈夫、他们的儿子、他们的女儿以及爱丽丝的兄弟卷入了一起谋杀案。五人中的一人杀死了另外四人中的一人。以下事实涉及上述五个人:

Alice, Alice’s husband, their son, their daughter, and Alice’s brother were involved in a murder. One of the five killed one of the other four. The following facts refer to the five people mentioned:

  1. 谋杀案发生时,一名男子和一名女子正一起在酒吧里。

  2. A man and a woman were together in a bar at the time of the murder.

  3. 案发时,受害者和凶手正一起在海滩上。

  4. The victim and the killer were together on a beach at the time of the murder.

  5. 谋杀案发生时,爱丽丝的两个孩子中有一个是独自一人。

  6. One of Alice’s two children was alone at the time of the murder.

  7. 谋杀案发生时,爱丽丝和她的丈夫并不在一起。

  8. Alice and her husband were not together at the time of the murder.

  9. 受害者的双胞胎姐妹并不是凶手。

  10. The victim’s twin was not the killer.

  11. 凶手比受害者年轻。

  12. The killer was younger than the victim.

这五个人中哪一个是受害者?

Which one of the five was the victim?


花点时间尝试找出解决方案。(你应该假设受害者的双胞胎是上述五个人之一。)萨默斯的书提供了以下提示:“首先找到谋杀案发生时两对人的位置,然后确定凶手和受害者是谁,这样就不会出现矛盾的情况。”

Take some time to try to work out a solution. (You should assume that the victim’s twin is one of the five people mentioned.) Summers’ book offers the following hint: “First find the locations of two pairs of people at the time of the murder, and then determine who the killer and the victim were so that no condition is contradicted.”

2.2.解决方案

2.2. A Solution

如果您曾经解答过这个谜题,那么您可能已经注意到一些事情。首先,绘制图表并系统地寻找答案是有帮助的。角色、位置和属性的数量是有限的,因此需要考虑的可能“事态”只有有限个。这些数字也足够小,因此系统地搜索所有可能性虽然很乏味,但最终会让您找到正确答案。这是这类逻辑谜题的一个特殊功能;例如,您不会期望通过遍历所有可能性来证明每个大于 2 的偶数都可以写成素数之和。

If you have worked on the puzzle, you may have noticed a few things. First, it is helpful to draw a diagram, and to be systematic about searching for an answer. The number of characters, locations, and attributes is finite, so that there are only finitely many possible “states of affairs” that need to be considered. The numbers are also small enough so that systematic search through all the possibilities, though tedious, will eventually get you to the right answer. This is a special feature of logic puzzles like this; you would not expect to show, for example, that every even number greater than two can be written as a sum of primes by running through all the possibilities.

您可能还注意到,这个问题似乎预设了这个问题有一个唯一的答案,也就是说,在所有符合条件的情况中,只有一个人可能是凶手。先验地,如果没有这个假设,找到某个可能是受害者的人和证明这个人一定是受害者之间是有区别的。换句话说,展示符合条件的情况和最终证明没有其他解决方案之间是有区别的。

Another thing that you may have noticed is that the question seems to presuppose that there is a unique answer to the question, which is to say, over all the states of affairs that meet the list of conditions, there is only one person who can possibly be the killer. A priori, without that assumption, there is a difference between finding some person who could have been the victim and showing that that person had to be the victim. In other words, there is a difference between exhibiting some state of affairs that meets the criteria and demonstrating conclusively that no other solution is possible.

书中公布的解决方案不仅产生了符合标准的情况,而且同时证明了这是唯一符合标准的情况。全文引述如下。

The published solution in the book not only produces a state of affairs that meets the criterion, but at the same time proves that this is the only one that does so. It is quoted below, in full.


从(1)、(2)、(3)中,五个人的角色分别是:酒吧里的男人和女人,海滩上的杀手和受害者,以及单独的孩子。

From (1), (2), and (3), the roles of the five people were as follows: Man and Woman in the bar, Killer and Victim on the beach, and Child alone.

然后,从 (4) 来看,要么爱丽丝的丈夫在酒吧而爱丽丝在海滩上,要么爱丽丝在酒吧而爱丽丝的丈夫在海滩上。

Then, from (4), either Alice’s husband was in the bar and Alice was on the beach, or Alice was in the bar and Alice’s husband was on the beach.

如果爱丽丝的丈夫在酒吧里,和他在一起的女人是他的女儿,独自一人的孩子是他的儿子,而爱丽丝和她的兄弟在海滩上。那么爱丽丝或她的兄弟就是受害者;所以另一个就是凶手。但是,从(5)来看,受害者有一个双胞胎,而这个双胞胎是无辜的。由于爱丽丝和她的兄弟只能是彼此的双胞胎,这种情况是不可能的。因此爱丽丝的丈夫不在酒吧里。

If Alice’s husband was in the bar, the woman he was with was his daughter, the child who was alone was his son, and Alice and her brother were on the beach. Then either Alice or her brother was the victim; so the other was the killer. But, from (5), the victim had a twin, and this twin was innocent. Since Alice and her brother could only be twins to each other, this situation is impossible. Therefore Alice’s husband was not in the bar.

所以 Alice 在酒吧里。如果 Alice 在酒吧里,那她一定是和她的兄弟或儿子在一起的。

So Alice was in the bar. If Alice was in the bar, she was with her brother or her son.

如果爱丽丝和她哥哥在一起,那么她的丈夫就会带着两个孩子中的一个在海滩上。从(5)来看,受害者不可能是她的丈夫,因为其他人都不可能是他的双胞胎;所以凶手是她的丈夫,受害者是他身边的孩子。但这种情况是不可能的,因为它与(6)相矛盾。因此,爱丽丝没有和她的哥哥一起在酒吧里。

If Alice was with her brother, her husband was on the beach with one of the two children. From (5), the victim could not be her husband, because none of the others could be his twin; so the killer was her husband and the victim was the child he was with. But this situation is impossible, because it contradicts (6). Therefore, Alice was not with her brother in the bar.

所以爱丽丝和她的儿子在酒吧里。那么那个独自一人的孩子就是她的女儿。因此,爱丽丝的丈夫和爱丽丝的兄弟一起在海滩上。从之前的推理来看,受害者不可能是爱丽丝的丈夫。但受害者可能是爱丽丝的兄弟,因为爱丽丝可能是他的双胞胎。

So Alice was with her son in the bar. Then the child who was alone was her daughter. Therefore, Alice’s husband was with Alice’s brother on the beach. From previous reasoning, the victim could not be Alice’s husband. But the victim could be Alice’s brother because Alice could be his twin.

所以爱丽丝的兄弟是受害者,而爱丽丝的丈夫是凶手。

So Alice’s brother was the victim and Alice’s husband was the killer.


这一论证依赖于一些“逻辑之外”的因素,例如,父亲不可能比孩子年轻,父母和孩子不可能是双胞胎。但这一论证还涉及许多常见的逻辑术语和相关的推理模式。在下一节中,我们将重点讨论上述论证中出现的一些关键逻辑术语,如“和”、“或”、“不”和“如果……那么”等词。

This argument relies on some “extralogical” elements, for example, that a father cannot be younger than his child, and that a parent and his or her child cannot be twins. But the argument also involves a number of common logical terms and associated patterns of inference. In the next section, we will focus on some of the key logical terms occurring in the argument above, words like “and,” “or,” “not,” and “if … then.”

我们的目标是解释控制这些术语使用的推理模式。为此,我们将使用符号逻辑的方法引入变量ABC,…代表基本陈述或命题和符号¬, 和分别代表“和”、“或”、“非”和“如果……那么……”。这样做可以让我们专注于复合语句是如何利用逻辑术语从基本语句构建而成的,同时抽象出具体的内容。我们还将采用一种程式化的符号来表示推理规则铭文

Our goal is to give an account of the patterns of inference that govern the use of those terms. To that end, using the methods of symbolic logic, we will introduce variables A, B, C, … to stand for fundamental statements, or propositions, and symbols , , ¬, and to stand for “and,” “or,” “not,” and “if … then … ,” respectively. Doing so will let us focus on the way that compound statements are built up from basic ones using the logical terms, while abstracting away from the specific content. We will also adopt a stylized notation for representing inferences as rules: the inscription

表示该语句C是合乎逻辑的结果AB

indicates that statement C is a logical consequence of A and B.

2.3.推理规则

2.3. Rules of Inference

2.3.1.含义

2.3.1. Implication

我们将要讨论的第一个推理模式涉及“如果……那么……”结构,可能很难辨别。它的使用在很大程度上隐含在上述解决方案中。第四段中的推理更详细地阐述如下:

The first pattern of inference we will discuss, involving the “if … then …” construct, can be hard to discern. Its use is largely implicit in the solution above. The inference in the fourth paragraph, spelled out in greater detail, runs as follows:


如果爱丽丝在酒吧,那么爱丽丝就和她的兄弟或儿子在一起。

If Alice was in the bar, Alice was with her brother or her son.

爱丽丝在酒吧里。

Alice was in the bar.

爱丽丝和她的兄弟或儿子在一起。

Alice was with her brother or son.


这条规则有时被称为肯定前件法,即“蕴涵消除法”,因为它告诉我们如何在论证中使用蕴涵。一般来说,它表达如下:

This rule is sometimes known as modus ponens, or “implication elimination,” since it tells us how to use an implication in an argument. As a rule, it is expressed as follows:

读这句话的意思是,如果你有证据AB,可能来自一些假设,以及A,可能来自假设,然后结合这些假设得出B,来自两个子证明中的假设。

Read this as saying that if you have a proof of AB, possibly from some hypotheses, and a proof of A, possibly from hypotheses, then combining these yields a proof of B, from the hypotheses in both subproofs.

推导“如果……那么”陈述的规则更加微妙。考虑第三段的开头,其中论证了如果爱丽丝的丈夫在酒吧,那么爱丽丝或她的兄弟就是受害者。抽象出一些细节,该论点具有以下形式:

The rule for deriving an “if … then” statement is more subtle. Consider the beginning of the third paragraph, which argues that if Alice’s husband was in the bar, then Alice or her brother was the victim. Abstracting away some of the details, the argument has the following form:


假设爱丽丝的丈夫在酒吧。

Suppose Alice’s husband was in the bar.

然后 …

Then …

然后 …

Then …

那么爱丽丝或她的兄弟就是受害者。

Then Alice or her brother was the victim.

因此,如果爱丽丝的丈夫在酒吧,那么爱丽丝或她的兄弟就是受害者。

Thus, if Alice’s husband was in the bar, then Alice or her brother was the victim.


这是一种假设推理。假设A成立,我们认为B也成立。如果我们成功了,我们已经证明了A暗示B,不假设A换句话说,暂时假设A通过在结论中明确说明,成立被“取消”。

This is a form of hypothetical reasoning. On the supposition that A holds, we argue that B holds as well. If we are successful, we have shown that A implies B, without supposing A. In other words, the temporary assumption that A holds is “canceled” by making it explicit in the conclusion.

假设被贴上标签1;当应用引入规则时,标签1表示相关假设。假设上方的线表示该假设已被引入规则“取消”。

The hypothesis is given the label 1; when the introduction rule is applied, the label 1 indicates the relevant hypothesis. The line over the hypothesis indicates that the assumption has been “canceled” by the introduction rule.

2.3.2.连词

2.3.2. Conjunction

和蕴涵一样,其他逻辑联结词通常以引入消除规则为特征。引入规则显示如何建立涉及联结词的断言,而消除规则显示如何使用包含联结词的断言导出其他断言。

As was the case for implication, other logical connectives are generally characterized by their introduction and elimination rules. An introduction rule shows how to establish a claim involving the connective, while an elimination rule shows how to use such a statement that contains the connective to derive others.

例如,让我们考虑一下连词的情况,即单词“and”。非正式地,我们通过建立每个连词来建立连词。例如,非正式地,我们可以论证:

Let us consider, for example, the case of conjunction, that is, the word “and.” Informally, we establish a conjunction by establishing each conjunct. For example, informally we might argue:


受害者是爱丽丝的哥哥。

Alice’s brother was the victim.

爱丽丝的丈夫就是凶手。

Alice’s husband was the killer.

因此,爱丽丝的兄弟是受害者,而爱丽丝的丈夫是凶手。

Therefore Alice’s brother was the victim and Alice’s husband was the killer.


这个推论似乎太明显了,无法明确说明,因为“和”这个词只是将两个断言合并为一个。非正式证明通常会淡化这种区别。在符号逻辑中,规则如下:

The inference seems almost too obvious to state explicitly, since the word “and” simply combines the two assertions into one. Informal proofs often downplay the distinction. In symbolic logic, the rule reads as follows:

通过两个消除规则,我们可以提取两个组成部分:

The two elimination rules allow us to extract the two components:


爱丽丝的丈夫在酒吧,而爱丽丝则在海滩上。

Alice’s husband was in the bar and Alice was on the beach.

原来爱丽丝的丈夫在酒吧里。

So Alice’s husband was in the bar.


或者:

Or:


爱丽丝的丈夫在酒吧,而爱丽丝则在海滩上。

Alice’s husband was in the bar and Alice was on the beach.

所以爱丽丝就在海滩上。

So Alice was on the beach.


在符号中,这些模式呈现如下:

In symbols, these patterns are rendered as follows:

这里lr代表“左”和“右”。

Here the l and r stand for “left” and “right”.

2.3.3.否定与假

2.3.3. Negation and Falsity

从逻辑上讲,证明“非 A”就等于证明 A 会导致矛盾。例如:

In logical terms, showing “not A” amounts to showing that A leads to a contradiction. For example:


假设爱丽丝的丈夫在酒吧。

Suppose Alice’s husband was in the bar.

这种情况是不可能的。

This situation is impossible.

因此爱丽丝的丈夫不在酒吧。

Therefore Alice’s husband was not in the bar.


这是另一种假设推理形式,类似于建立“如果……那么”语句时使用的推理:我们暂时假设 A,表明这会导致矛盾,并得出结论“非 A”成立。用符号表示,规则如下:

This is another form of hypothetical reasoning, similar to that used in establishing an “if … then” statement: we temporarily assume A, show that leads to a contradiction, and conclude that “not A” holds. In symbols, the rule reads as follows:

消除规则与这些规则是对偶的。它表示如果我们同时拥有“A”和“非 A”,那么我们就有矛盾。这种模式在下面的非正式论证中得到了说明,它隐含在“Malice and Alice”解决方案的第四段中。

The elimination rule is dual to these. It expresses that if we have both “A” and “not A,” then we have a contradiction. This pattern is illustrated in the informal argument below, which is implicit in the fourth paragraph of the solution to “Malice and Alice.”


凶手是艾丽丝的丈夫,受害者是他怀里的孩子。

The killer was Alice’s husband and the victim was the child he was with.

因此凶手并不比受害者年轻。

So the killer was not younger than his victim.

但根据(6),凶手比受害者年轻。

But according to (6), the killer was younger than his victim.

这种情况是不可能的。

This situation is impossible.


在符号逻辑中,推理规则表示如下:

In symbolic logic, the rule of inference is expressed as follows:

还要注意,在符号框架中,我们引入了一个新符号,它对应于自然语言短语,如“这是一个矛盾”或“这是不可能的”。

Notice also that in the symbolic framework, we have introduced a new symbol, . It corresponds to natural language phrases like “this is a contradiction” or “this is impossible.”

有哪些规则? 在下一章我们将要介绍的证明系统中,没有引入规则;“假”就是假,除了从矛盾的假设中提取它之外,没有其他方法可以证明它。另一方面,该系统提供了一条规则,允许我们从矛盾中得出任何结论:

What are the rules governing ? In the proof system we will introduce in the next chapter, there is no introduction rule; “false” is false, and there should be no way to prove it, other than extract it from contradictory hypotheses. On the other hand, the system provides a rule that allows us to conclude anything from a contradiction:

消除规则也有一个花哨的拉丁名,ex falso sequitur quodlibet,意思是“任何你想要的结果都源于虚假”。

The elimination rule also has the fancy Latin name, ex falso sequitur quodlibet, which means “anything you want follows from falsity.”

从自然语言的角度来看,这种消除规则很难激发,但尽管如此,它对于捕捉常见的推理模式还是必要的。理解它的一种方法是这样的。考虑以下语句:

This elimination rule is harder to motivate from a natural language perspective, but, nonetheless, it is needed to capture common patterns of inference. One way to understand it is this. Consider the following statement:


对于每个自然数n, 如果n为素数且大于 2,则n很奇怪。

For every natural number n, if n is prime and greater than 2, then n is odd.


我们想说这是一个正确的陈述。但如果它是真的,那么对于任何特定的数字来说,它都是正确的n. 服用n=2,我们有以下陈述:

We would like to say that this is a true statement. But if it is true, then it is true of any particular number n. Taking n=2, we have the statement:


如果 2 为质数且大于 2,则 2 为奇数。

If 2 is prime and greater than 2, then 2 is odd.


在这个条件语句中,前件和后件都是假的。我们承诺说这个语句是真的,这一事实表明我们应该能够以某种方式证明,语句 2 是奇数,这是由错误语句 2 是素数且大于 2 得出的。前件假也巧妙地概括了这种推理。

In this conditional statement, both the antecedent and succedent are false. The fact that we are committed to saying that this statement is true shows that we should be able to prove, one way or another, that the statement 2 is odd follows from the false statement that 2 is prime and greater than 2. The ex falso neatly encapsulates this sort of inference.

请注意,如果我们定义¬AA,那么否定的引入和消除规则无非就是蕴涵的引入和消除。我们可以想到¬A生动地表达为“如果A是真的,那么猪就有翅膀”,其中“猪有翅膀”代表

Notice that if we define ¬A to be A, then the rules for negation introduction and elimination are nothing more than implication introduction and elimination, respectively. We can think of ¬A expressed colorfully by saying “if A is true, then pigs have wings,” where “pigs have wings” stands for .

引入了表示“假”的符号后,再引入表示“真”的符号才是公平的。与“假”相反,“真”没有消除规则,只有引入规则:

Having introduced a symbol for “false,” it is only fair to introduce a symbol for “true.” In contrast to “false,” “true” has no elimination rule, only an introduction rule:

简单来说,“真”就是真。

Put simply, “true” is true.

2.3.4.析取

2.3.4. Disjunction

析取(也称为“或”)的引入规则很简单。例如,在提出的解决方案中,条件 (3) 得到满足的说法可以证明如下:

The introduction rules for disjunction, otherwise known as “or,” are straightforward. For example, the claim that condition (3) is met in the proposed solution can be justified as follows:


谋杀案发生时,爱丽丝的女儿独自一人。

Alice’s daughter was alone at the time of the murder.

因此,要么爱丽丝的女儿在谋杀案发生时独自一人,要么爱丽丝的儿子在谋杀案发生时独自一人。

Therefore, either Alice’s daughter was alone at the time of the murder, or Alice’s son was alone at the time of the murder.


从象征意义上来说,这两条引入规则如下:

In symbolic terms, the two introduction rules are as follows:

在这里,再次lr代表“左”和“右”。

Here, again, the l and r stand for “left” and “right”.

析取消元法规则比较复杂,但它代表了一种基于案例的假设推理的自然形式。在“恶意和爱丽丝”的解决方案中出现的情况都是此规则的特殊情况,因此,构造一个新的例子来说明普遍现象将很有帮助。假设,在上面的论证中,我们已经确定爱丽丝的兄弟或儿子在酒吧,并且我们想为她的丈夫在海滩上的结论辩护。一种选择是通过案例进行辩论:首先,考虑她兄弟在酒吧的情况,并根据该假设为结论辩护;然后考虑她儿子在酒吧的情况,并根据第二个假设为同一结论辩护。由于这两个案例是详尽无遗的,如果我们知道结论在每种情况下都成立,我们就知道它完全成立。模式看起来像这样:

The disjunction elimination rule is trickier, but it represents a natural form of case-based hypothetical reasoning. The instances that occur in the solution to “Malice and Alice” are all special cases of this rule, so it will be helpful to make up a new example to illustrate the general phenomenon. Suppose, in the argument above, we had established that either Alice’s brother or her son was in the bar, and we wanted to argue for the conclusion that her husband was on the beach. One option is to argue by cases: first, consider the case that her brother was in the bar, and argue for the conclusion on the basis of that assumption; then consider the case that her son was in the bar, and argue for the same conclusion, this time on the basis of the second assumption. Since the two cases are exhaustive, if we know that the conclusion holds in each case, we know that it holds outright. The pattern looks something like this:


要么是爱丽丝的兄弟在酒吧里,要么是爱丽丝的儿子在酒吧里。

Either Alice’s brother was in the bar, or Alice’s son was in the bar.

假设,在第一种情况下,她的兄弟在酒吧。那么... 因此,她的丈夫在海滩上。

Suppose, in the first case, that her brother was in the bar. Then … Therefore, her husband was on the beach.

另一方面,假设她的儿子在酒吧。在这种情况下,…因此,在这种情况下,她的丈夫也在海滩上。

On the other hand, suppose her son was in the bar. In that case, … Therefore, in this case also, her husband was on the beach.

无论如何,我们已经确定她的丈夫当时在海滩上。

Either way, we have established that her husband was on the beach.


在符号中,此模式表达如下:

In symbols, this pattern is expressed as follows:

这种模式令人困惑的地方在于,它需要两个嵌套的假设推理实例:在第一个括号中,我们暂时假设A,在第二个区块中,我们暂时假设B尘埃落定后,我们已确立C彻底地。

What makes this pattern confusing is that it requires two instances of nested hypothetical reasoning: in the first block of parentheses, we temporarily assume A, and in the second block, we temporarily assume B. When the dust settles, we have established C outright.

还有另一种通常与“或”一起使用的推理模式,如下例所示:

There is another pattern of reasoning that is commonly used with “or,” as in the following example:


要么是爱丽丝的丈夫在酒吧,要么是爱丽丝在酒吧。

Either Alice’s husband was in the bar, or Alice was in the bar.

爱丽丝的丈夫不在酒吧。

Alice’s husband was not in the bar.

所以爱丽丝在酒吧里。

So Alice was in the bar.


用符号表示的话,我们将此规则表达如下:

In symbols, we would render this rule as follows:

下一章我们将看到,可以从其他规则中推导出这条规则。因此,我们不会其作为我们系统中的基本推理规则。

We will see in the next chapter that it is possible to derive this rule from the others. As a result, we will not take this to be a fundamental rule of inference in our system.

2.3.5.当且仅当

2.3.5. If and only if

在数学论证中,通常会说两个陈述,AB, 那 ”A当且仅当B成立。”这个断言有时缩写为“A当且仅当B”,意思是A暗示BB暗示A。我们没有必要在我们的逻辑语言中引入一个新的符号来模拟这个连接词,因为这个陈述可以像我们刚才所做的那样,用“暗示”和“和”来表达。但请注意,表达式的长度加倍了,因为AB每一个都是重复的。因此,逻辑缩写既方便又自然。

In mathematical arguments, it is common to say of two statements, A and B, that “A holds if and only if B holds.” This assertion is sometimes abbreviated “A iff B,” and means simply that A implies B and B implies A. It is not essential that we introduce a new symbol into our logical language to model this connective, since the statement can be expressed, as we just did, in terms of “implies” and “and.” But notice that the length of the expression doubles because A and B are each repeated. The logical abbreviation is therefore convenient, as well as natural.

“恶意与爱丽丝”的条件意味着爱丽丝在酒吧当且仅当爱丽丝的丈夫在海滩上。通过依次论证每个蕴涵,可以建立这样的陈述:

The conditions of “Malice and Alice” imply that Alice is in the bar if and only if Alice’s husband is on the beach. Such a statement is established by arguing for each implication in turn:


我声称爱丽丝在酒吧当且仅当爱丽丝的丈夫在海滩上。

I claim that Alice is in the bar if and only if Alice’s husband is on the beach.

为了理解这一点,首先假设爱丽丝在酒吧里。

To see this, first suppose that Alice is in the bar.

然后 …

Then …

因此爱丽丝的丈夫就在海滩上。

Hence Alice’s husband is on the beach.

相反,假设爱丽丝的丈夫在海滩上。

Conversely, suppose Alice’s husband is on the beach.

然后 …

Then …

因此爱丽丝就在酒吧里。

Hence Alice is in the bar.


请注意,在这个例子中,我们改变了陈述的形式,首先陈述结论,而不是在论证结束时陈述。这种“指示”在非正式论证中很常见,因为它有助于引导读者的期望并预示论证的发展方向。正式的演绎系统通常不会模拟这种细微差别,这一事实标志着正式论证和非正式论证之间的差异,我们将在下文中讨论这个话题。

Notice that with this example, we have varied the form of presentation, stating the conclusion first, rather than at the end of the argument. This kind of “signposting” is common in informal arguments, in that is helps guide the reader’s expectations and foreshadow where the argument is going. The fact that formal systems of deduction do not generally model such nuances marks a difference between formal and informal arguments, a topic we will return to below.

引言以自然演绎建模如下:

The introduction is modeled in natural deduction as follows:

iff 的消元规则并不复杂。用非正式的语言来说,这是“左”规则:

The elimination rules for iff are unexciting. In informal language, here is the “left” rule:


当且仅当爱丽丝的丈夫在海滩上时,爱丽丝才会在酒吧里。

Alice is in the bar if and only if Alice’s husband is on the beach.

爱丽丝在酒吧里。

Alice is in the bar.

因此,爱丽丝的丈夫就在海滩上。

Hence, Alice’s husband is on the beach.


而“正确”的规则恰恰相反。

The “right” rule simply runs in the opposite direction.


当且仅当爱丽丝的丈夫在海滩上时,爱丽丝才会在酒吧里。

Alice is in the bar if and only if Alice’s husband is on the beach.

爱丽丝的丈夫在海滩上。

Alice’s husband is on the beach.

因此,爱丽丝在酒吧里。

Hence, Alice is in the bar.


根据自然演绎推理,规则如下:

Rendered in natural deduction, the rules are as follows:

2.3.6.矛盾证明

2.3.6. Proof by Contradiction

我们看到了一个非正式论证的例子,它隐式地使用了否定的引入规则:

We saw an example of an informal argument that implicitly uses the introduction rule for negation:


假设爱丽丝的丈夫在酒吧。

Suppose Alice’s husband was in the bar.

这种情况是不可能的。

This situation is impossible.

因此爱丽丝的丈夫不在酒吧。

Therefore Alice’s husband was not in the bar.


考虑以下论点:

Consider the following argument:


假设爱丽丝的丈夫不在海滩上。

Suppose Alice’s husband was not on the beach.

这种情况是不可能的。

This situation is impossible.

因此爱丽丝的丈夫就在海滩上。

Therefore Alice’s husband was on the beach.


乍一看,你可能会认为这个论证遵循与前一个论证相同的模式。但仔细观察就会发现一个区别:在第一个论证中,结论中引入了一个否定,而在第二个论证中,否定从假设中被消除。使用否定引入来结束第二个论证将得出结论“爱丽丝的丈夫不在海滩上的情况并非如此。”用肯定的陈述“爱丽丝的丈夫海滩上”取代结论的推理规则称为矛盾证明。(它还有一个花哨的名字,即“归谬法”,即“归谬法”)。

At first glance, you might think this argument follows the same pattern as the one before. But a closer look should reveal a difference: in the first argument, a negation is introduced into the conclusion, whereas in the second, it is eliminated from the hypothesis. Using negation introduction to close the second argument would yield the conclusion “It is not the case that Alice’s husband was not on the beach.” The rule of inference that replaces the conclusion with the positive statement that Alice’s husband was on the beach is called a proof by contradiction. (It also has a fancy name, reductio ad absurdum, “reduction to an absurdity.”)

可能很难看出这两条规则之间的区别,因为我们通常认为“爱丽丝的丈夫不在海滩上”这句话是爱丽丝的丈夫在海滩上的拐弯抹角和近乎不合语法的说法。事实上,这条规则相当于增加了一条公理,即对于每个语句 A,“不不是 A”等同于 A。

It may be hard to see the difference between the two rules, because we commonly take the statement “Alice’s husband was not not on the beach” to be a roundabout and borderline ungrammatical way of saying that Alice’s husband was on the beach. Indeed, the rule is equivalent to adding an axiom that says that for every statement A, “not not A” is equivalent to A.

有一种数学方法被称为“构造性数学”,它否定“非非 A”与 A 的等价性。构造性论证往往具有更好的计算解释;某事为真的证明应该提供明确的证据证明该陈述为真,而不是提供该陈述不可能为假的证据。我们将在后面的章节中讨论构造性推理。尽管如此,矛盾证明在当代数学中被广泛使用,因此,在此期间,我们将自由使用矛盾证明作为我们的基本规则之一。

There is a style of doing mathematics known as “constructive mathematics” that denies the equivalence of “not not A” and A. Constructive arguments tend to have much better computational interpretations; a proof that something is true should provide explicit evidence that the statement is true, rather than evidence that it can’t possibly be false. We will discuss constructive reasoning in a later chapter. Nonetheless, proof by contradiction is used extensively in contemporary mathematics, and so, in the meanwhile, we will use proof by contradiction freely as one of our basic rules.

在自然演绎中,矛盾证明用以下模式表达:

In natural deduction, proof by contradiction is expressed by the following pattern:

假设¬A在最终推理时被取消。

The assumption ¬A is canceled at the final inference.

2.4.命题逻辑的语言

2.4. The Language of Propositional Logic

命题逻辑的语言始于符号ABC,……旨在涵盖基本断言或命题,这些断言或命题可以是真或假。复合表达式由括号和上一节介绍的逻辑符号组成。例如,

The language of propositional logic starts with symbols A, B, C, … which are intended to range over basic assertions, or propositions, which can be true or false. Compound expressions are built up using parentheses and the logical symbols introduced in the last section. For example,

((A(¬B))¬(CD))

是命题公式的一个例子。

is an example of a propositional formula.

在用符号逻辑编写表达式时,我们将采用一种运算顺序,这样我们就可以删除多余的括号。解析表达式时:

When writing expressions in symbolic logic, we will adopt an order of operations which allows us to drop superfluous parentheses. When parsing an expression:

  • 否定的结合最为紧密。

  • Negation binds most tightly.

  • 然后,连词和析取词从右到左绑定。

  • Then, conjunctions and disjunctions bind from right to left.

  • 最后,蕴涵和双重蕴涵从右到左绑定。

  • Finally, implications and bi-implications bind from right to left.

例如,表达式¬ABCD被理解为((¬A)B)(CD)

So, for example, the expression ¬ABCD is understood as ((¬A)B)(CD).

例如,假设我们分配以下变量:

For example, suppose we assign the following variables:

  • A:爱丽丝的丈夫在酒吧里

  • A: Alice’s husband was in the bar

  • B:爱丽丝在海滩上

  • B: Alice was on the beach

  • C:爱丽丝在酒吧里

  • C: Alice was in the bar

  • D:爱丽丝的丈夫在海滩上

  • D: Alice’s husband was on the beach

那么“要么爱丽丝的丈夫在酒吧,爱丽丝在海滩上,或者爱丽丝在酒吧,爱丽丝的丈夫在海滩上”这句话将被表达为

Then the statement “either Alice’s husband was in the bar and Alice was on the beach, or Alice was in the bar and Alice’s husband was on the beach” would be rendered as

(AB)(CD).

然而,有时合适的翻译并不那么简单。由于自然语言更加灵活和细致入微,因此需要一定程度的抽象和规范来进行翻译。有时不同的翻译可以说是合理的。在令人满意的情况下,替代翻译在逻辑上是等价的,即人们可以使用纯逻辑规则从另一个翻译中得出一个翻译。在不太令人满意的情况下,翻译将不等价,在这种情况下,从逻辑的角度来看,原始陈述只是模棱两可的。在这种情况下,选择符号表示有助于澄清预期的含义。

Sometimes the appropriate translation is not so straightforward, however. Because natural language is more flexible and nuanced, a degree of abstraction and regimentation is needed to carry out the translation. Sometimes different translations are arguably reasonable. In happy situations, alternative translations will be logically equivalent, in the sense that one can derive each from the other using purely logical rules. In less happy situations, the translations will not be equivalent, in which case the original statement is simply ambiguous, from a logical point of view. In cases like that, choosing a symbolic representation helps clarify the intended meaning.

例如,考虑这样一句话:“爱丽丝和她的儿子在海滩上,但她的丈夫独自一人。”我们可以选择以下变量:

Consider, for example, a statement like “Alice was with her son on the beach, but her husband was alone.” We might choose variables as follows:

  • A:爱丽丝在海滩上

  • A: Alice was on the beach

  • B:爱丽丝的儿子在海滩上

  • B: Alice’s son was on the beach

  • C:爱丽丝的丈夫独自一人

  • C: Alice’s husband was alone

在这种情况下,我们可以用符号表示语句为ABC。使用“with”一词似乎不仅仅意味着爱丽丝和她的儿子都在海滩上;例如,它似乎意味着他们意识到彼此的存在、互动等。同样,虽然我们已经翻译了“but”和“and”,但“but”一词也传达了信息;在这种情况下,它似乎强调了对比,而在其他情况下,它可以用来断言与预期相反的事实。因此,在这两种情况下,逻辑渲染都对原始句子的某些特征进行建模,同时抽象其他特征。

In that case, we might represent the statement in symbols as ABC. Using the word “with” may seem to connote more than the fact that Alice and her son were both on the beach; for example, it seems to connote that they aware of each others’ presence, interacting, etc. Similarly, although we have translated the word “but” and “and,” the word “but” also convey information; in this case, it seems to emphasize a contrast, while in other situations, it can be used to assert a fact that is contrary to expectations. In both cases, then, the logical rendering models certain features of the original sentence while abstracting others.

2.5.练习

2.5. Exercises

  1. 这是乔治·J·萨默斯 (George J. Summers) 的另一个(可怕的)逻辑谜题,名为“家庭谋杀案”。

    一天晚上,一对父母和他们的儿子和女儿的家里发生了一起谋杀案。一名家庭成员谋杀了另一名家庭成员,第三名家庭成员目击了整个过程,第四名家庭成员则是事后共犯。

    1. 同案犯与证人性别相反。

    2. 最年长的成员与证人是异性。

    3. 最年轻的成员与受害者是异性。

    4. 该附属品的年龄比受害者的年龄还要大。

    5. 父亲是成员中年龄最大的。

    6. 凶手并不是最年轻的成员。

    父亲、母亲、儿子、女儿这四个人中,哪一个是凶手?

    解决这个难题,并写出一个清晰的论据来证明你的答案是正确的。

  2. Here is another (gruesome) logic puzzle by George J. Summers, called “Murder in the Family.”

    Murder occurred one evening in the home of a father and mother and their son and daughter. One member of the family murdered another member, the third member witnessed the crime, and the fourth member was an accessory after the fact.

    1. The accessory and the witness were of opposite sex.

    2. The oldest member and the witness were of opposite sex.

    3. The youngest member and the victim were of opposite sex.

    4. The accessory was older than the victim.

    5. The father was the oldest member.

    6. The murderer was not the youngest member.

    Which of the four—father, mother, son, or daughter—was the murderer?

    Solve this puzzle, and write a clear argument to establish that your answer is correct.

  3. 使用助记符F(父亲),M(母亲),D(女儿),S(儿子),Mu(凶手),V(受害者),W(证人),A(配饰),O(最老的),Y(最年轻),我们可以定义命题变量,如FM(父亲是凶手)DV(女儿是受害者)FO(父亲是长子),VY(受害者是最年轻的)等等。请注意,只有儿子或女儿可以是最小的,只有母亲或父亲可以是最年长的。

    按照这些约定,第一个线索可以表示为

    ((FASA)(MWDW))((MADA)(FWSW)),

    换句话说,如果父亲或儿子是从犯,那么母亲或女儿就是证人,反之亦然。以类似的方式表示其他五条线索。

    表示第四条线索比较棘手。尝试写下一个公式,描述信息未排除的所有可能性。

  4. Using the mnemonic F (Father), M (Mother), D (Daughter), S (Son), Mu (Murderer), V (Victim), W (Witness), A (Accessory), O (Oldest), Y (Youngest), we can define propositional variables like FM (Father is the Murderer), DV (Daughter is the Victim), FO (Father is Oldest), VY (Victim is Youngest), etc. Notice that only the son or daughter can be the youngest, and only the mother or father can be the oldest.

    With these conventions, the first clue can be represented as

    ((FASA)(MWDW))((MADA)(FWSW)),

    in other words, if the father or son was the accessory, then the mother or daughter was the witness, and vice-versa. Represent the other five clues in a similar manner.

    Representing the fourth clue is tricky. Try to write down a formula that describes all the possibilities that are not ruled out by the information.

  5. 考虑以下三个假设:

    • 艾伦喜欢袋鼠,而贝蒂喜欢青蛙,或者卡尔喜欢仓鼠。

    • 如果贝蒂喜欢青蛙,那么艾伦就不喜欢袋鼠。

    • 如果卡尔喜欢仓鼠,那么贝蒂就喜欢青蛙。

    写一个清晰的论据来表明这三个假设是矛盾的。

  6. Consider the following three hypotheses:

    • Alan likes kangaroos, and either Betty likes frogs or Carl likes hamsters.

    • If Betty likes frogs, then Alan doesn’t like kangaroos.

    • If Carl likes hamsters, then Betty likes frogs.

    Write a clear argument to show that these three hypotheses are contradictory.

3.命题逻辑的自然演绎

3. Natural Deduction for Propositional Logic

回顾上一章的论证,我们发现,直观地说,有些推论是有效的,有些则不是。例如,如果在推理链中,我们建立了“AB” 似乎完全可以合理地得出这样的结论B.如果我们建立AB,以及“如果AB然后C”可以合理地得出这样的结论C。另一方面,如果我们建立“A或者B”我们没有理由得出这样的结论B没有更多信息。

Reflecting on the arguments in the previous chapter, we see that, intuitively speaking, some inferences are valid and some are not. For example, if, in a chain of reasoning, we had established “A and B,” it would seem perfectly reasonable to conclude B. If we had established A, B, and “If A and B then C,” it would be reasonable to conclude C. On the other hand, if we had established “A or B,” we would not be justified in concluding B without further information.

符号逻辑的任务是开发一种精确的数学理论,解释哪些推论是有效的以及为什么有效。有两种一般方法来阐明有效性的概念。在本章中,我们将考虑演绎方法:如果推论可以通过反映所涉及逻辑术语含义的基本推理规则来证明,则该推论是有效的。在第 6 章中,我们将考虑“语义”方法:如果推论是模式的一个实例,并且该模式始终从真实的假设中得出真实的结论,则该推论是有效的。

The task of symbolic logic is to develop a precise mathematical theory that explains which inferences are valid and why. There are two general approaches to spelling out the notion of validity. In this chapter, we will consider the deductive approach: an inference is valid if it can be justified by fundamental rules of reasoning that reflect the meaning of the logical terms involved. In Chapter 6 we will consider the “semantic” approach: an inference is valid if it is an instance of a pattern that always yields a true conclusion from true hypotheses.

3.1.自然演绎中的推导

3.1. Derivations in Natural Deduction

我们已经看到,命题逻辑的语言允许我们从命题变量构建表达式A,B,C,使用命题连接词,如, 和¬。我们现在将考虑一个正式的演绎系统,我们可以用它来证明命题公式。有许多这样的系统可供选择;我们将使用的系统称为自然演绎,由 Gerhard Gentzen 在 20 世纪 30 年代设计。

We have seen that the language of propositional logic allows us to build up expressions from propositional variables A,B,C, using propositional connectives like , , , and ¬. We will now consider a formal deductive system that we can use to prove propositional formulas. There are a number of such systems on offer; the one will use is called natural deduction, designed by Gerhard Gentzen in the 1930s.

在自然演绎中,每个证明都是基于假设的证明。换句话说,在任何证明中,都有一组有限的假设{B,C,}结论A,证明表明A遵循B,C,

In natural deduction, every proof is a proof from hypotheses. In other words, in any proof, there is a finite set of hypotheses {B,C,} and a conclusion A, and what the proof shows is that A follows from B,C,.

和公式一样,证明也是通过按照规则将较小的证明组合在一起而建立的。例如,阅读 and-introduction 规则的方法

Like formulas, proofs are built by putting together smaller proofs, according to the rules. For instance, the way to read the and-introduction rule

如下:如果你有证据P1A从一些假设中,你得到了一个证据P2B然后你可以用这个规则把它们放在一起,得到一个证明AB,它使用了P1以及所有假设P2。例如,这是(AB)(AC)从三个假设来看,AB, 和C

is as follows: if you have a proof P1 of A from some hypotheses, and you have a proof P2 of B from some hypotheses, then you can put them together using this rule to obtain a proof of AB, which uses all the hypotheses in P1 together with all the hypotheses in P2. For example, this is a proof of (AB)(AC) from three hypotheses, A, B, and C:

在一些自然演绎的表示中,证明被写成一系列行,其中每一行都可以引用任何前面的行来证明。但在这里我们将采用严格的二维图解格式,其中每个推论的前提都出现在结论的正上方。这使得检查证明并检查其是否正确变得容易:每个推论都应该是用特定公式实例化规则之一中的字母的结果。

In some presentations of natural deduction, a proof is written as a sequence of lines in which each line can refer to any previous lines for justification. But here we will adopt a rigid two-dimensional diagrammatic format in which the premises of each inference appear immediately above the conclusion. This makes it easy to look over a proof and check that it is correct: each inference should be the result of instantiating the letters in one of the rules with particular formulas.

使自然演绎令人困惑的一件事是,当你以这种方式整理证明时,假设可以被消除,或者,正如我们所说的,被取消。例如,我们可以将隐含引入规则应用于最后一个证明,并得到以下证明B(AB)(AC)仅从两个假设来看,AC

One thing that makes natural deduction confusing is that when you put together proofs in this way, hypotheses can be eliminated, or, as we will say, canceled. For example, we can apply the implies-introduction rule to the last proof, and obtain the following proof of B(AB)(AC) from only two hypotheses, A and C:

这里,我们使用标签 1 来表示假设B已取消。任何标签都可以,但我们倾向于使用数字。

Here, we have used the label 1 to indicate the place where the hypothesis B was canceled. Any label will do, though we will tend to use numbers for that purpose.

我们可以继续取消假设A

We can continue to cancel the hypothesis A:

结果是仅使用假设的证明C我们也可以继续取消这个假设:

The result is a proof using only the hypothesis C. We can continue to cancel that hypothesis as well:

最终的证明根本不使用任何假设。换句话说,它直接确立了结论。

The resulting proof uses no hypothesis at all. In other words, it establishes the conclusion outright.

请注意,在第二步中,我们取消了假设的两个“副本”A。在自然演绎中,我们可以选择取消哪些假设;我们可以取消其中一个假设,而保留另一个假设事实上,我们也可以执行蕴涵引入规则并取消零个假设。例如,以下是AB从假设B

Notice that in the second step, we canceled two “copies” of the hypothesis A. In natural deduction, we can choose which hypotheses to cancel; we could have canceled either one, and left the other hypothesis open. In fact, we can also carry out the implication-introduction rule and cancel zero hypotheses. For example, the following is a short proof of AB from the hypothesis B:

在这个证明中,A均被取消。

In this proof, zero copies of A are canceled.

还请注意,尽管我们使用的字母AB, 和C作为命题变量,在上面的证明中,我们可以用任何命题公式代替它们。例如,我们可以用A根据公式(DE)处处,并且仍然有正确的证明。在某些逻辑表述中,命题变量和任意命题公式使用不同的字母,但我们将继续模糊区别。你可以认为AB, 和C代表命题变量或公式,随你喜欢。如果你把它们看作命题变量,请记住,在任何规则或证明中,你可以用不同的公式替换每个变量,而仍然有有效的规则或证明。

Also notice that although we are using letters like A, B, and C as propositional variables, in the proofs above we can replace them by any propositional formula. For example, we can replace A by the formula (DE) everywhere, and still have correct proofs. In some presentations of logic, different letters are used for propositional variables and arbitrary propositional formulas, but we will continue to blur the distinction. You can think of A, B, and C as standing for propositional variables or formulas, as you prefer. If you think of them as propositional variables, just keep in mind that in any rule or proof, you can replace every variable by a different formula, and still have a valid rule or proof.

最后,还要注意的是,在这些例子中,我们假设了一个特殊规则作为构建证明的起点。它被称为假设规则,它看起来像这样:

Finally, notice also that in these examples, we have assumed a special rule as the starting point for building proofs. It is called the assumption rule, and it looks like this:

这意味着在任何时候我们都可以自由地假设一个公式,A. 单一配方A构成一行证明,阅读此证明的方式如下:假设A,我们已经证明A

What it means is that at any point we are free to simply assume a formula, A. The single formula A constitutes a one-line proof, and the way to read this proof is as follows: assuming A, we have proved A.

其余的推理规则在上一章中给出,我们在此进行总结。

The remaining rules of inference were given in the last chapter, and we summarize them here.

含义:

Implication:

连词:

Conjunction:

否定:

Negation:

析取:

Disjunction:

真与假:

Truth and falsity:

双重含义:

Bi-implication:

归谬法(反证法):

Reductio ad absurdum (proof by contradiction):

3.2.示例

3.2. Examples

让我们再考虑一些自然演绎证明的例子。在每种情况下,你都应该思考公式说了什么,以及每一步调用了哪条推理规则。还要密切注意每个阶段取消了哪些假设。如果你查看树的任何一个节点,那么在这一点上已经确定的是,该主张遵循它上面所有尚未取消的假设。

Let us consider some more examples of natural deduction proofs. In each case, you should think about what the formulas say and which rule of inference is invoked at each step. Also pay close attention to which hypotheses are canceled at each stage. If you look at any node of the tree, what has been established at that point is that the claim follows from all the hypotheses above it that haven’t been canceled yet.

以下是证明ACABBC

The following is a proof of AC from AB and BC:

直观地看,公式

Intuitively, the formula

(AB)(BC)(AC)

“内化”了先前证明的结论。符号用于组合假设,并且符号用来表示右边是左边的结果。下面是该公式的证明:

“internalizes” the conclusion of the previous proof. The symbol is used to combine hypotheses, and the symbol is used to express that the right-hand side is a consequence of the left. Here is a proof of that formula:

下一个证明表明,如果结论C,得出AB,那么就可以从它们的合取中得出。

The next proof shows that if a conclusion, C, follows from A and B, then it follows from their conjunction.

下一个证明的结论可以解释为,如果其中一个不是A或者B为真,则它们都是假的。它说明了否定规则的用法。

The conclusion of the next proof can be interpreted as saying that if it is not the case that one of A or B is true, then they are both false. It illustrates the use of the rules for negation.

最后,接下来的两个例子说明了ex falso规则的用法。第一个是任意公式的推导B¬AA

Finally, the next two examples illustrate the use of the ex falso rule. The first is a derivation of an arbitrary formula B from ¬A and A:

第二项显示B遵循A¬AB

The second shows that B follows from A and ¬AB:

在一些证明系统中,这些规则被视为系统的一部分。但是在我们的系统中我们不需要这样做:这两个例子表明这些规则可以从我们的其他规则中推导出来

In some proof systems, these rules are taken to be part of the system. But we do not need to that with our system: these two examples show that the rules can be derived from our other rules.

3.3.正向和反向推理

3.3. Forward and Backward Reasoning

自然演绎应该代表我们在处理逻辑谜题时使用的推理和论证模式的理想化模型,例如,在上一章中。它们之间存在明显的区别:我们用符号和二维图表描述自然演绎证明,而我们的非正式论证则用文字和段落来写。值得反思一下该模型捕捉到了什么自然演绎应该阐明我们逻辑论证的形式结构,描述证明结论的适当方法,并解释我们使用的规则在何种意义上是有效的。

Natural deduction is supposed to represent an idealized model of the patterns of reasoning and argumentation we use, for example, when working with logic puzzles as in the last chapter. There are obvious differences: we describe natural deduction proofs with symbols and two-dimensional diagrams, whereas our informal arguments are written with words and paragraphs. It is worthwhile to reflect on what is captured by the model. Natural deduction is supposed to clarify the form and structure of our logical arguments, describe the appropriate means of justifying a conclusion, and explain the sense in which the rules we use are valid.

构建自然演绎证明可能会令人困惑,但思考它为什么令人困惑是有帮助的。例如,我们可以决定自然演绎不是逻辑推理的好模型。或者我们可能会得出结论,自然演绎的令人困惑的特征告诉我们一些关于普通论证的有趣之处。

Constructing natural deduction proofs can be confusing, but it is helpful to think about why it is confusing. We could, for example, decide that natural deduction is not a good model for logical reasoning. Or we might come to the conclusion that the features of natural deduction that make it confusing tell us something interesting about ordinary arguments.

在“官方”描述中,自然演绎证明是通过将较小的证明放在一起得到较大的证明来构建的。为了证明ABBA,我们从假设开始AB。然后我们分别构造以下两个证明:

In the “official” description, natural deduction proofs are constructed by putting smaller proofs together to obtain bigger ones. To prove ABBA, we start with the hypothesis AB. Then we construct, separately, the following two proofs:

然后我们利用这两个证明构造以下证明:

Then we use these two proofs to construct the following one:

最后,我们将隐含引入规则应用于该证明以取消假设并得到所需的结论:

Finally, we apply the implies-introduction rule to this proof to cancel the hypothesis and obtain the desired conclusion:

该过程与非正式争论中发生的过程类似,我们从一些假设开始,然后逐步得出结论。

The process is similar to what happens in an informal argument, where we start with some hypotheses, and work forward towards a conclusion.


假设苏珊很高并且约翰很高兴。

Suppose Susan is tall and John is happy.

那么,约翰尤其高兴。

Then, in particular, John is happy.

此外,苏珊很高。

Also, Susan is tall.

所以约翰很高兴,苏珊也长高了。

So John is happy and Susan is tall.

因此,我们表明,如果苏珊身高较高且约翰很高兴,那么约翰也会很高兴且苏珊身高较高。

Therefore we have shown that if Susan is tall and John is happy, then John is happy and Susan is tall.


然而,当我们阅读自然演绎证明时,我们经常会倒着读。首先,我们查看底部以查看要证明的内容。然后我们考虑用于证明它的规则,并查看规则要求的前提。然后我们查看如何证明这些主张,等等。同样,当我们构建自然演绎证明时,我们通常也会倒着读:我们从要证明的主张开始,将其放在底部,然后寻找要应用的规则。

However, when we read natural deduction proofs, we often read them backward. First, we look at the bottom to see what is being proved. Then we consider the rule that is used to prove it, and see what premises the rule demands. Then we look to see how those claims are proved, and so on. Similarly, when we construct a natural deduction proof, we typically work backward as well: we start with the claim we are trying to prove, put that at the bottom, and look for rules to apply.

有时这个过程会崩溃。假设我们只剩下一个目标,那就是一个命题变量,A。没有可以应用的引入规则,因此,除非A是一个假设,它必须来自一个消除规则。但这没有明确问题:也许A通过将消去规则应用于AB或将或消除规则应用于CCA。此时,我们会考虑假设,并开始向前推进。例如,如果我们的假设CCAB,然后我们将继续努力获得ABA

At times that process breaks down. Suppose we are left with a goal that is a single propositional variable, A. There are no introduction rules that can be applied, so, unless A is a hypothesis, it has to come from an elimination rule. But that underspecifies the problem: perhaps the A comes from applying the and-elimination rule to AB, or from applying the or-elimination rule to C and CA. At that point, we look to the hypotheses, and start working forward. If, for example, our hypotheses are C and CAB, we would then work forward to obtain AB and A.

因此,存在一种用于证明自然演绎定理的一般启发式方法:

There is thus a general heuristic for proving theorems in natural deduction:

  1. 首先从结论开始,使用引言规则。例如,如果你试图证明以下形式的陈述AB, 添加A你的假设列表,并尝试推导出B. 如果你试图证明以下形式的陈述AB,使用 and-introduction 规则将你的任务简化为证明A,然后证明B

  2. Start by working backward from the conclusion, using the introduction rules. For example, if you are trying to prove a statement of the form AB, add A to your list of hypotheses and try to derive B. If you are trying to prove a statement of the form AB, use the and-introduction rule to reduce your task to proving A, and then proving B.

  3. 当你在第一步中没有事情要做时,使用消除规则来继续前进。如果你有假设ABA,运用肯定前件推导出B. 如果你有一个假设AB,使用或消除法来拆分案例,考虑A在一个案例中B在另一个。

  4. When you have run out things to do in the first step, use elimination rules to work forward. If you have hypotheses AB and A, apply modus ponens to derive B. If you have a hypothesis AB, use or-elimination to split on cases, considering A in one case and B in the other.

在第 5 章中,我们将向此列表添加另一个元素:如果其他方法都失败了,请尝试反证法。

In Chapter 5 we will add one more element to this list: if all else fails, try a proof by contradiction.

正向推理和逆向推理之间的矛盾也存在于数学和其他领域的非正式论证中。当我们证明一个定理时,我们通常会使用假设、假设、定义和背景知识进行正向推理。但我们也会牢记目标,这有助于我们理解前进的步骤。

The tension between forward and backward reasoning is found in informal arguments as well, in mathematics and elsewhere. When we prove a theorem, we typically reason forward, using assumptions, hypotheses, definitions, and background knowledge. But we also keep the goal in mind, and that helps us make sense of the forward steps.

当我们转向交互式定理证明时,我们会看到精益具有支持正向和反向推理的机制。这些机制在非正式论证风格和自然演绎模型之间架起了一座桥梁,从而更清楚地展示了正在发生的事情。

When we turn to interactive theorem proving, we will see that Lean has mechanisms to support both forward and backward reasoning. These form a bridge between informal styles of argumentation and the natural deduction model, and thereby provide a clearer picture of what is going on.

自然演绎证明的另一个令人困惑的特征是,每个假设都有一个范围,也就是说,在证明中只有某些点可以使用假设。当然,这也是非正式数学论证的一个特点。假设一个段落以“让x是小于 100 的任何数字”,认为x最多有五个质因数,并得出结论“因此我们证明了每个小于 100 的数字最多有五个因数。”参考“x”,并且小于 100 的假设仅在该段落的范围内有效。如果下一段以“现在假设x是大于 100 的任何数字”,那么当然,假设x小于 100 不再适用。

Another confusing feature of natural deduction proofs is that every hypothesis has a scope, which is to say, there are only certain points in the proof where an assumption is available for use. Of course, this is also a feature of informal mathematical arguments. Suppose a paragraph begins “Let x be any number less than 100,” argues that x has at most five prime factors, and concludes “thus we have shown that every number less than 100 has at most five factors.” The reference “x”, and the assumption that it is less than 100, is only active within the scope of the paragraph. If the next paragraph begins with the phrase “Now suppose x is any number greater than 100,” then, of course, the assumption that x is less than 100 no longer applies.

在自然演绎中,假设从被假定的点到被取消的点都是可用的。我们将看到,交互式定理证明语言也有确定参考和假设范围的机制,这些机制也揭示了非形式数学中的范围问题。

In natural deduction, a hypothesis is available from the point where it is assumed until the point where it is canceled. We will see that interactive theorem proving languages also have mechanisms to determine the scope of references and hypotheses, and that these, too, shed light on scoping issues in informal mathematics.

3.4.案例推理

3.4. Reasoning by Cases

消除析取的规则令人困惑,但我们可以通过一个例子来理解它。考虑以下非正式论证:

The rule for eliminating a disjunction is confusing, but we can make sense of it with an example. Consider the following informal argument:


乔治要么在家,要么在校园。

George is either at home or on campus.

如果他在家,他就在学习。

If he is at home, he is studying.

如果他在校园,他就和他的朋友在一起。

If he is on campus, he is with his friends.

因此,乔治要么在学习,要么和他的朋友在一起。

Therefore, George is either studying or with his friends.


A是乔治在家的陈述,让B是乔治在校园的声明,让C是乔治正在研究的陈述,并设D是乔治和他的朋友在一起的陈述。那么上面的论证有以下模式:从ABAC, 和BD,总结CD。在自然演绎中,我们无法一步就得出这个结论,但将其充实为适当的证明并不需要太多工作。非正式地,我们必须进行如下论证。

Let A be the statement that George is at home, let B be the statement that George is on campus, let C be the statement that George is studying, and let D be the statement the George is with his friends. Then the argument above has the following pattern: from AB, AC, and BD, conclude CD. In natural deduction, we cannot get away with drawing this conclusion in a single step, but it does not take too much work to flesh it out into a proper proof. Informally, we have to argue as follows.


乔治要么在家,要么在校园。

Georges is either at home or on campus.

情况 1:假设他在家。我们知道如果他在家,那么他就在学习。所以,在这种情况下,他正在学习。因此,在这种情况下,他要么在学习,要么在和朋友在一起。

案例 2:假设他在校园里。我们知道,如果他在校园里,那么他就和他的朋友在一起。所以,在这种情况下,他和他的朋友在一起。因此,在这种情况下,他要么在学习,要么和他的朋友在一起。

Case 1: Suppose he is at home. We know that if he is at home, then he is studying. So, in this case, he is studying. Therefore, in this case, he is either studying or with his friends.

Case 2: Suppose he is on campus. We know that if he is on campus, then he is with his friends. So, in this case, he is with his friends. Therefore, in this case, he is either studying or with his friends.

无论怎样,乔治要么在学习,要么和他的朋友在一起。

Either way, George is either studying or with his friends.


自然演绎证明如下:

The natural deduction proof looks as follows:

你应该思考这个证明的结构如何反映其上方的非正式的基于案例的论点。

You should think about how the structure of this proof reflects the informal case-based argument above it.

再举一个例子,这是A(BC)(AB)(AC)

For another example, here is a proof of A(BC)(AB)(AC):

3.5.一些逻辑身份

3.5. Some Logical Identities

两个命题公式,AB如果AB是可证明的。逻辑等价类似于恒等式,例如x+y=y+x代数中经常出现的公式。具体来说,可以证明如果两个公式等价,那么可以在任何公式中用一个公式代替另一个公式,结果也将等价。(一些证明系统将此视为基本规则,交互式定理证明器可以适应它,但我们不会其视为自然演绎的基本规则。)

Two propositional formulas, A and B, are said to be logically equivalent if AB is provable. Logical equivalences are similar to identities like x+y=y+x that occur in algebra. In particular, one can show that if two formulas are equivalent, then one can substitute one for the other in any formula, and the results will also be equivalent. (Some proof systems take this to be a basic rule, and interactive theorem provers can accommodate it, but we will not take it to be a fundamental rule of natural deduction.)

作为参考,以下列表包含一些常用的命题等价关系以及一些值得注意的公式。想想为什么直观上这些公式应该是正确的。

For reference, the following list contains some commonly used propositional equivalences, along with some noteworthy formulas. Think about why, intuitively, these formulas should be true.

  1. 交换性ABBA

  2. Commutativity of : ABBA

  3. 交换性ABBA

  4. Commutativity of : ABBA

  5. 结合性(AB)CA(BC)

  6. Associativity of : (AB)CA(BC)

  7. 结合性 (AB)CA(BC)

  8. Associativity of (AB)CA(BC)

  9. 分配性超过A(BC)(AB)(AC)

  10. Distributivity of over : A(BC)(AB)(AC)

  11. 分配性超过A(BC)(AB)(AC)

  12. Distributivity of over : A(BC)(AB)(AC)

  13. (A(BC))(ABC)

  14. (A(BC))(ABC).

  15. (AB)((BC)(AC))

  16. (AB)((BC)(AC))

  17. ((AB)C)(AC)(BC)

  18. ((AB)C)(AC)(BC)

  19. ¬(AB)¬A¬B

  20. ¬(AB)¬A¬B

  21. ¬(AB)¬A¬B

  22. ¬(AB)¬A¬B

  23. ¬(A¬A)

  24. ¬(A¬A)

  25. ¬(AB)A¬B

  26. ¬(AB)A¬B

  27. ¬A(AB)

  28. ¬A(AB)

  29. (¬AB)(AB)

  30. (¬AB)(AB)

  31. AA

  32. AA

  33. A

  34. A

  35. A¬A

  36. A¬A

  37. ¬(A¬A)

  38. ¬(A¬A)

  39. (AB)(¬B¬A)

  40. (AB)(¬B¬A)

  41. (ACD)((AC)(AD))

  42. (ACD)((AC)(AD))

  43. (((AB)A)A)

  44. (((AB)A)A)

所有这些都可以通过使用第 3.1 节中列出的基本规则在自然演绎中得出。但其中一些需要使用归谬法规则或反证法,我们尚未详细讨论过。我们将在第 5 章中讨论此规则的使用以及其他经典逻辑模式。

All of these can be derived in natural deduction using the fundamental rules listed in Section 3.1. But some of them require the use of the reductio ad absurdum rule, or proof by contradiction, which we have not yet discussed in detail. We will discuss the use of this rule, and other patterns of classical logic, in the Chapter 5.

3.6.练习

3.6. Exercises

在自然演绎中构建证明时,仅使用第 3.1 节中给出的规则列表。

When constructing proofs in natural deduction, use only the list of rules given in Section 3.1.

  1. 给出自然演绎证明AB来自假设BA

  2. Give a natural deduction proof of AB from hypothesis BA.

  3. 给出自然演绎证明(QR)R来自假设Q

  4. Give a natural deduction proof of (QR)R from hypothesis Q.

  5. 给出自然演绎证明¬(AB)(A¬B)

  6. Give a natural deduction proof of ¬(AB)(A¬B).

  7. 给出自然演绎证明QS来自假设(PQ)RST

  8. Give a natural deduction proof of QS from hypotheses (PQ)R and ST.

  9. 给出自然演绎证明(AC)(B¬C)¬(AB)

  10. Give a natural deduction proof of (AC)(B¬C)¬(AB).

  11. 给出自然演绎证明(AB)((AC)¬(B¬C))

  12. Give a natural deduction proof of (AB)((AC)¬(B¬C)).

  13. 再看一下上一章的练习 3。使用命题变量AB, 和C分别针对“艾伦喜欢袋鼠”“贝蒂喜欢青蛙”和“卡尔喜欢仓鼠”这三个假设,将这三个假设表达为符号公式,然后通过自然演绎从中得出矛盾。

  14. Take another look at Exercise 3 in the last chapter. Using propositional variables A, B, and C for “Alan likes kangaroos,” “Betty likes frogs” and “Carl likes hamsters,” respectively, express the three hypotheses as symbolic formulas, and then derive a contradiction from them in natural deduction.

  15. 给出自然演绎证明ABBA

  16. Give a natural deduction proof of ABBA.

  17. 给出自然演绎证明¬A¬B¬(AB)

  18. Give a natural deduction proof of ¬A¬B¬(AB)

  19. 给出自然演绎证明¬(AB)¬A¬B.(你不需要使用反证法。)

  20. Give a natural deduction proof of ¬(AB) from ¬A¬B. (You do not need to use proof by contradiction.)

  21. 给出自然演绎证明¬(A¬A)

  22. Give a natural deduction proof of ¬(A¬A).

  23. 给出自然演绎证明(¬A¬B)来自假设AB

  24. Give a natural deduction proof of (¬A¬B) from hypothesis AB.

  25. 给出自然演绎证明PR来自假设(PQ)R. 这与证明((PQ)R)(PR)

  26. Give a natural deduction proof of PR from hypothesis (PQ)R. How does this differ from a proof of ((PQ)R)(PR)?

  27. 给出自然演绎证明C(AB)C来自假设AB

  28. Give a natural deduction proof of C(AB)C from hypothesis AB.

  29. 给出自然演绎证明WYXZ来自假设WXYZ

  30. Give a natural deduction proof of WYXZ from hypotheses WX and YZ.

  31. 给出自然演绎证明(A(BA))A

  32. Give a natural deduction proof of (A(BA))A.

4.精益中的命题逻辑

4. Propositional Logic in Lean

在本章中,您将学习如何使用 Lean 编写证明。我们将从纯机械翻译开始,这将使您能够用 Lean 表示任何自然演绎证明。然而,我们将看到,这种编写证明的风格不是很直观,也不会产生非常易读的证明。它也不能很好地扩展。

In this chapter, you will learn how to write proofs in Lean. We will start with a purely mechanical translation that will enable you to represent any natural deduction proof in Lean. We will see, however, that such a style of writing proofs is not very intuitive, nor does it yield very readable proofs. It also does not scale well.

然后,我们将考虑精益提供的一些支持更具前瞻性的论证风格的机制。由于这些证明看起来更像非正式证明,但可以直接转化为自然推理,它们将帮助我们理解两者之间的关系。

We will then consider some mechanisms that Lean offers that support a more forward-directed style of argumentation. Since these proofs look more like informal proofs but can be directly translated to natural deduction, they will help us understand the relationship between the two.

4.1.命题和证明的表达式

4.1. Expressions for Propositions and Proofs

从本质上讲,Lean 是一种类型检查器。这意味着我们可以编写表达式并要求系统检查它们是否格式正确,还可以要求系统告诉我们它们表示什么类型的对象。试试这个:

At its core, Lean is what is known as a type checker. This means that we can write expressions and ask the system to check that they are well formed, and also ask the system to tell us what type of object they denote. Try this:

variables A B C : Prop

#check A  ¬ B  C
variables A B C : Prop

#check A  ¬ B  C

在该文本的在线版本中,您可以按“试试看!”按钮将示例复制到编辑器窗口,然后将鼠标悬停在文本上的标记上以阅读消息。

In the online version of this text, you can press the “try it!” button to copy the example to an editor window, and then hover over the markers on the text to read the messages.

在示例中,我们声明了三个遍历命题的变量,并要求 Lean 检查表达式。该命令的输出是,它断言的类型为。在 Lean 中,每个格式正确的表达式都有一个类型。A ¬ B C#checkA ¬ B C : PropA ¬ B CProp

In the example, we declare three variables ranging over propositions, and ask Lean to check the expression A ¬ B C. The output of the #check command is A ¬ B C : Prop, which asserts that A ¬ B C is of type Prop. In Lean, every well-formed expression has a type.

逻辑连接词以 unicode 呈现。下表向您展示了如何在编辑器中输入这些符号,还为纯粹主义者提供了 ascii 等效符号。

The logical connectives are rendered in unicode. The following chart shows you how you can type these symbols in the editor, and also provides ascii equivalents, for the purists among you.

统一码

Unicode

ASCII

Ascii

精益投入

Lean input

真的

true

错误的

false

¬

¬

不是

not

\not\neg

\not, \neg

/\

/\

\and

\and

\/

\/

\or

\or

->

->

\to,,\r\imp

\to, \r, \imp

<->

<->

\iff\lr

\iff, \lr

全部

forall

\all

\all

存在

exists

\ex

\ex

λ

λ

乐趣

fun

\lam\fun

\lam, \fun

~=

~=

\ne

\ne

到目前为止,我们只讨论了列表中的前七项。稍后我们将讨论量词、lambda 和相等性。尝试输入一些表达式并自行检查。您应该尝试将上例中的一个变量更改为D,或在表达式中插入一个无意义的符号,然后查看 Lean 返回的错误消息。

So far, we have only talked about the first seven items on the list. We will discuss the quantifiers, lambda, and equality later. Try typing some expressions and checking them on your own. You should try changing one of the variables in the example above to D, or inserting a nonsense symbol into the expression, and take a look at the error message that Lean returns.

除了声明变量之外,如果P是任何类型的表达式,我们还可以声明为真的Prop假设:P

In addition to declaring variables, if P is any expression of type Prop, we can declare the hypothesis that P is true:

variables A B : Prop
variable  h : A  ¬ B

#check h
variables A B : Prop
variable  h : A  ¬ B

#check h

形式上,任何命题都可以看作是一种类型,即该命题的证明类型。假设或前提只是该类型的变量。然后,建立证明就是写下正确类型的表达式。例如,如果 是P类型的任何表达式,则是 类型的表达式,是 类型的表达式。换句话说,如果是 的证明, 是通过应用 和 的左消去规则得到的证明的名称:A Band.left PAand.right PBPA Band.left P

Formally, what is going on is that any proposition can be viewed as a type, namely, the type of proofs of that proposition. A hypothesis, or premise, is just a variable of that type. Building proofs is then a matter of writing down expressions of the correct type. For example, if P is any expression of type A B, then and.left P is an expression of type A, and and.right P is an expression of type B. In other words, if P is a proof of A B, and and.left P is a name for the proof you get by applying the left elimination rule for and:

类似地,是通过应用正确的消去规则得到的证明。因此,继续上面的例子,我们可以写and.right PB

Similarly, and.right P is the proof of B you get by applying the right elimination rule. So, continuing the example above, we can write

variables A B : Prop
variable h : A  ¬ B

#check and.left h
#check and.right h
variables A B : Prop
variable h : A  ¬ B

#check and.left h
#check and.right h

这两个表达式分别代表这两个证明:

The two expressions represent, respectively, these two proofs:

请注意,在这种表示自然演绎证明的方式中,没有“自由浮动”的假设。每个假设都有一个标签。在精益中,我们通常使用诸如hh1h2、 等表达式来标记假设,但您可以使用任何您想要的标识符。

Notice that in this way of representing natural deduction proofs, there are no “free floating” hypotheses. Every hypothesis has a label. In Lean, we will typically use expressions like h, h1, h2, … to label hypotheses, but you can use any identifier you want.

如果h1是 的证明Ah2是 的证明B, 则是 的证明。因此我们可以继续上面的例子:and.intro h1 h2A B

If h1 is a proof of A and h2 is a proof of B, then and.intro h1 h2 is a proof of A B. So we can continue the example above:

variables A B : Prop
variable h : A  ¬ B

#check and.intro (and.right h) (and.left h)
variables A B : Prop
variable h : A  ¬ B

#check and.intro (and.right h) (and.left h)

这对应于以下证明:

This corresponds to the following proof:

那么蕴涵呢?消去规则很简单:如果P₁是的证明,并且是的证明,那么就是的证明。请注意,我们甚至不需要命名规则:只需写下后跟,就好像您将第一个应用于第二个一样。如果和是复合表达式,请在它们周围加上括号,以明确每个表达式的开始和结束位置。A BP₂AP₁ P₂BP₁P₂P₁P₂

What about implication? The elimination rule is easy: if P₁ is a proof of A B and P₂ is a proof of A then P₁ P₂ is a proof of B. Notice that we do not even need to name the rule: you just write P₁ followed by P₂, as though you are applying the first to the second. If P₁ and P₂ are compound expressions, put parentheses around them to make it clear where each one begins and ends.

variables A B C D : Prop

variable h1 : A  (B  C)
variable h2 : D  A
variable h3 : D
variable h4 : B

#check h2 h3
#check h1 (h2 h3)
#check (h1 (h2 h3)) h4
variables A B C D : Prop

variable h1 : A  (B  C)
variable h2 : D  A
variable h3 : D
variable h4 : B

#check h2 h3
#check h1 (h2 h3)
#check (h1 (h2 h3)) h4

Lean 采用了应用程序与左关联的惯例,因此表达式被解释为。蕴涵与关联,因此被解释为。这看起来可能很有趣,但它是一种表示采用多个假设的蕴涵的方便方法,因为表达式意味着从、、和得出。因此,上面的例子可以写成如下形式:h1 h2 h3(h1 h2) h3A B CA (B C)A B C D EEABCD

Lean adopts the convention that applications associate to the left, so that an expression h1 h2 h3 is interpreted as (h1 h2) h3. Implications associate to the right, so that A B C is interpreted as A (B C). This may seem funny, but it is a convenient way to represent implications that take multiple hypotheses, since an expression A B C D E means that E follows from A, B, C, and D. So the example above could be written as follows:

variables A B C D : Prop

variable h1 : A  B  C
variable h2 : D  A
variable h3 : D
variable h4 : B

#check h2 h3
#check h1 (h2 h3)
#check h1 (h2 h3) h4
variables A B C D : Prop

variable h1 : A  B  C
variable h2 : D  A
variable h3 : D
variable h4 : B

#check h2 h3
#check h1 (h2 h3)
#check h1 (h2 h3) h4

请注意,表达式中仍然需要括号。h1 (h2 h3)

Notice that parentheses are still needed in the expression h1 (h2 h3).

蕴涵引入规则比较棘手,因为它可以取消假设。就精益表达式而言,该规则翻译如下。假设AB具有类型Prop,并且假设h是成立的前提AP是的证明B,可能涉及h。那么表达式就是的证明。例如,我们可以构造如下的证明:assume h : A, PA BA A A

The implication introduction rule is the tricky one, because it can cancel a hypothesis. In terms of Lean expressions, the rule translates as follows. Suppose A and B have type Prop, and, assuming h is the premise that A holds, P is proof of B, possibly involving h. Then the expression assume h : A, P is a proof of A B. For example, we can construct a proof of A A A as follows:

variable A : Prop

#check (assume h : A, and.intro h h)
variable A : Prop

#check (assume h : A, and.intro h h)

请注意,我们不再需要将其声明A为前提。该词assume使前提成为括号中表达式的局部,并且在做出假设后,我们可以引用h。假设是,是的证明,因此表达式是的证明。在这种情况下,我们可以省略括号,因为表达式是明确的:h : Aand.intro h hA Aassume h : A, and.intro h hA A A

Notice that we no longer have to declare A as a premise. The word assume makes the premise local to the expression in parentheses, and after the assumption is made, we can refer to h. Given the assumption h : A, and.intro h h is a proof of A A, and so the expression assume h : A, and.intro h h is a proof of A A A. In this case, we could leave out the parentheses because the expression is unambiguous:

variable A : Prop

#check assume h : A, and.intro h h
variable A : Prop

#check assume h : A, and.intro h h

上面,我们从前提证明了 。我们可以得到如下的证明:¬ B AA ¬ BA ¬ B ¬ B A

Above, we proved ¬ B A from the premise A ¬ B. We can instead obtain a proof of A ¬ B ¬ B A as follows:

variables A B : Prop
#check (assume h : A  ¬ B, and.intro (and.right h) (and.left h))
variables A B : Prop
#check (assume h : A  ¬ B, and.intro (and.right h) (and.left h))

我们所做的只是将前提转移到本地assume

All we did was move the premise into a local assume.

(顺便说一下,该assume命令只是 lambda 符号的替代语法,所以我们也可以这样写:

(By the way, the assume command is just alternative syntax for the lambda symbol, so we could also have written this:

variables A B : Prop
#check (λ h : A  ¬ B, and.intro (and.right h) (and.left h))
variables A B : Prop
#check (λ h : A  ¬ B, and.intro (and.right h) (and.left h))

稍后您将了解有关 lambda 符号的更多信息。)

You will learn more about the lambda symbol later.)

4.2.更多命令

4.2. More commands

让我们引入一个新的 Lean 命令,example。此命令告诉 Lean 您将要证明一个定理,或者更一般地,写下给定类型的表达式。然后应该跟随着证明或表达式本身。

Let us introduce a new Lean command, example. This command tells Lean that you are about to prove a theorem, or, more generally, write down an expression of the given type. It should then be followed by the proof or expression itself.

variables A B : Prop

example : A  ¬ B  ¬ B  A :=
assume h : A  ¬ B,
and.intro (and.right h) (and.left h)
variables A B : Prop

example : A  ¬ B  ¬ B  A :=
assume h : A  ¬ B,
and.intro (and.right h) (and.left h)

当给出此命令时,Lean 会检查后面的表达式:=并确保其类型正确。如果正确,它会接受该表达式作为有效证明。如果不是,它会引发错误。

When given this command, Lean checks the expression after the := and makes sure it has the right type. If so, it accepts the expression as a valid proof. If not, it raises an error.

由于该example命令提供了有关后面表达式(在本例中为被证明的命题)类型的信息,因此有时我们可以省略其他信息。例如,我们可以省略假设的类型:

Because the example command provides information as to the type of the expression that follows (in this case, the proposition being proved), it sometimes enables us to omit other information. For example, we can leave off the type of the assumption:

variables A B : Prop

example : A  ¬ B  ¬ B  A :=
assume h,
and.intro (and.right h) (and.left h)
variables A B : Prop

example : A  ¬ B  ¬ B  A :=
assume h,
and.intro (and.right h) (and.left h)

因为 Lean 知道我们试图用前提来证明一个蕴涵,所以它可以推断出当我们写作时,标识符标记了假设。A ¬ Bassume hhA ¬ B

Because Lean knows we are trying to prove an implication with premise A ¬ B, it can infer that when we write assume h, the identifier h labels the assumption A ¬ B.

我们也可以反过来,用 这个词为系统提供更多show信息。如果A是一个命题,P是一个证明,那么表达式“ ”与单独的 含义相同,但它表示 是的证明。当 Lean 检查这个表达式时,它会在解析它周围的表达式之前确认确实是 的证明。因此,在我们的例子中,我们也可以这样写:show A, from PPPAPA

We can also go in the other direction, and provide the system with more information, with the word show. If A is a proposition and P is a proof, the expression “show A, from P” means the same thing as P alone, but it signals the intention that P is a proof of A. When Lean checks this expression, it confirms that P really is a proof of A, before parsing the expression surrounding it. So, in our example, we could also write:

variables A B : Prop

example : A  ¬ B  ¬ B  A :=
assume h : A  ¬ B,
show ¬ B  A, from and.intro (and.right h) (and.left h)
variables A B : Prop

example : A  ¬ B  ¬ B  A :=
assume h : A  ¬ B,
show ¬ B  A, from and.intro (and.right h) (and.left h)

我们甚至可以注释较小的表达式和,如下所示:and.right hand.left h

We could even annotate the smaller expressions and.right h and and.left h, as follows:

variables A B : Prop

example : A  ¬ B  ¬ B  A :=
assume h : A  ¬ B,
show ¬ B  A, from and.intro
  (show ¬ B, from and.right h)
  (show A, from and.left h)
variables A B : Prop

example : A  ¬ B  ¬ B  A :=
assume h : A  ¬ B,
show ¬ B  A, from and.intro
  (show ¬ B, from and.right h)
  (show A, from and.left h)

这里值得一提的是,Lean 通常会忽略空格,例如缩进和回车。我们本可以将整个示例写在一行中。一般来说,我们将采用缩进和换行的惯例,以显示证明的结构并使其更易于阅读。

This is a good place to mention that Lean generally ignores whitespace, like indentation and returns. We could have written the entire example on a single line. In general, we will adopt conventions for indentation and line breaks that show the structure of proofs and make them easier to read.

尽管在上面的例子中show命令不是必需的,但使用这种风格有很多好的理由。首先,也许最重要的是,它使我们人类更容易阅读证明。其次,它使证明更容易编写如果你在证明中犯了一个错误,如果你明确表达了意图,Lean 就更容易找出你错在哪里并提供有意义的错误消息。最后,在子句中证明信息show通常使你能够省略其他地方的信息,因为 Lean 可以从你陈述的意图中推断出这些信息。

Although in the examples above the show commands were not necessary, there are a number of good reasons to use this style. First, and perhaps most importantly, it makes the proofs easier for us humans to read. Second, it makes the proofs easier to write: if you make a mistake in a proof, it is easier for Lean to figure out where you went wrong and provide a meaningful error message if you make your intentions clear. Finally, proving information in the show clause often makes it possible for you to omit information in other places, since Lean can infer that information from your stated intentions.

有符号变体。除了事先声明变量和前提之外,您还可以将它们作为示例的“参数”呈现,后跟冒号:

There are notational variants. Rather than declare variables and premises beforehand, you can also present them as “arguments” to the example, followed by a colon:

example (A B : Prop) : A  ¬ B  ¬ B  A :=
assume h : A  ¬ B,
show ¬ B  A, from and.intro (and.right h) (and.left h)
example (A B : Prop) : A  ¬ B  ¬ B  A :=
assume h : A  ¬ B,
show ¬ B  A, from and.intro (and.right h) (and.left h)

还有两个技巧可以帮助你在 Lean 中编写证明。第一个是使用sorry,这是 Lean 中的一个神奇术语,它可以提供任何事物的证明。它也被称为“作弊”。但作弊可以帮助你逐步构建合法的证明:如果 Lean 接受了带有 的证明sorry,那么你迄今为止编写的证明部分就通过了 Lean 的正确性检查。你需要做的就是sorry用真正的证明替换每个证明来完成任务。

There are two more tricks that can help you write proofs in Lean. The first is using sorry, which is a magical term in Lean which provides a proof of anything at all. It is also known as “cheating.” But cheating can help you construct legitimate proofs incrementally: if Lean accepts a proof with sorry’s, the parts of the proof you have written so far have passed Lean’s checks for correctness. All you need to do is replace each sorry with a real proof to complete the task.

variables A B : Prop

example : A  ¬ B  ¬ B  A :=
assume h, sorry

example : A  ¬ B  ¬ B  A :=
assume h, and.intro sorry sorry

example : A  ¬ B  ¬ B  A :=
assume h, and.intro (and.right h) sorry

example : A  ¬ B  ¬ B  A :=
assume h, and.intro (and.right h) (and.left h)
variables A B : Prop

example : A  ¬ B  ¬ B  A :=
assume h, sorry

example : A  ¬ B  ¬ B  A :=
assume h, and.intro sorry sorry

example : A  ¬ B  ¬ B  A :=
assume h, and.intro (and.right h) sorry

example : A  ¬ B  ¬ B  A :=
assume h, and.intro (and.right h) (and.left h)

第二个技巧是使用占位符,用下划线符号表示。当你在表达式中写下下划线时,你是在要求系统尝试为你填写值。这不足以调用全面的自动化来证明定理;相反,你是在要求 Lean 从上下文中推断出值。如果在应该有证明的地方使用下划线,Lean 通常不会填写证明,但会给你一个错误消息,告诉你缺少了什么。这将帮助你以向后驱动的方式逐步编写证明项。在上面的例子中,尝试sorry用下划线替换每个,_然后查看生成的错误消息。在每种情况下,错误都会告诉您需要填写什么,以及该阶段可用的变量和假设。

The second trick is the use of placeholders, represented by the underscore symbol. When you write an underscore in an expression, you are asking the system to try to fill in the value for you. This falls short of calling full-blown automation to prove a theorem; rather, you are asking Lean to infer the value from the context. If you use an underscore where a proof should be, Lean typically will not fill in the proof, but it will give you an error message that tells you what is missing. This will help you write proof terms incrementally, in a backward-driven fashion. In the example above, try replacing each sorry by an underscore, _, and take a look at the resulting error messages. In each case, the error tells you what needs to be filled in, and the variables and hypotheses that are available to you at that stage.

还有一个提示:如果你想要限制命令中引入的变量或前提的范围variables,请将它们放在以单词 开头section并以单词 结尾的块中end。我们将在下面使用这种机制。

One more tip: if you want to delimit the scope of variables or premises introduced with the variables command, put them in a block that begins with the word section and ends with the word end. We will use this mechanism below.

4.3.构建自然演绎证明

4.3. Building Natural Deduction Proofs

在本节中,我们通过为每个自然演绎规则提供翻译来描述自然演绎证明的机械翻译。我们已经看到了一些对应关系,但为了完整起见,我们在这里重复它们。

In this section, we describe a mechanical translation from natural deduction proofs, by giving a translation for each natural deduction rule. We have already seen some of the correspondences, but we repeat them all here, for completeness.

4.3.1.含义

4.3.1. Implication

我们已经解释过,蕴涵引入是用 来实现的assume,而蕴涵消除则写为应用。

We have already explained that implication introduction is implemented with assume, and implication elimination is written as application.

variables A B : Prop

example : A  B :=
assume h : A,
show B, from sorry

section
  variable h1 : A  B
  variable h2 : A

  example : B := h1 h2
end
variables A B : Prop

example : A  B :=
assume h : A,
show B, from sorry

section
  variable h1 : A  B
  variable h2 : A

  example : B := h1 h2
end

由于每个例子都是从声明必要的命题变量开始的,因此我们从今以后将在文中删除该声明。

Since every example begins by declaring the necessary propositional variables, we will henceforth suppress that declaration in the text.

4.3.2.连词

4.3.2. Conjunction

我们已经看到,与引入是用 实现的and.intro,并且消除规则是and.leftand.right

We have already seen that and-introduction is implemented with and.intro, and the elimination rules are and.left and and.right.

section
  variables (h1 : A) (h2 : B)

  example : A  B := and.intro h1 h2
end

section
  variable h : A  B

  example : A := and.left h
  example : B := and.right h
end
section
  variables (h1 : A) (h2 : B)

  example : A  B := and.intro h1 h2
end

section
  variable h : A  B

  example : A := and.left h
  example : B := and.right h
end

4.3.3.析取

4.3.3. Disjunction

或引入规则由or.inl和给出or.inr

The or-introduction rules are given by or.inl and or.inr.

section
  variable h : A

  example : A  B := or.inl h
end

section
  variable h : B

  example : A  B := or.inr h
end
section
  variable h : A

  example : A  B := or.inl h
end

section
  variable h : B

  example : A  B := or.inr h
end

消除规则比较棘手。为了证明C来自,你需要三个参数:来自 的证明、来自的证明和来自的证明。使用换行符和缩进来突出显示结构作为案例证明,我们可以用以下形式编写它:A BhA BCACB

The elimination rule is the tricky one. To prove C from A B, you need three arguments: a proof h of A B, a proof of C from A, and a proof of C from B. Using line breaks and indentation to highlight the structure as a proof by cases, we can write it with the following form:

section
  variable h : A  B
  variables (ha : A  C) (hb : B  C)
  example : C :=
  or.elim h
    (assume h1 : A,
      show C, from ha h1)
    (assume h1 : B,
      show C, from hb h1)
end
section
  variable h : A  B
  variables (ha : A  C) (hb : B  C)
  example : C :=
  or.elim h
    (assume h1 : A,
      show C, from ha h1)
    (assume h1 : B,
      show C, from hb h1)
end

请注意,我们可以在每个分支中重复使用标签h1,因为从概念上讲,这两个分支是不相交的。

Notice that we can reuse the label h1 in each branch, since, conceptually, the two branches are disjoint.

4.3.4.否定

4.3.4. Negation

在内部,否定定义为,你可以认为这意味着不可能的事情。因此,否定的规则与蕴涵的规则相似。要证明,假设并得出矛盾。¬ AA falseA¬ AA

Internally, negation ¬ A is defined by A false, which you can think of as saying that A implies something impossible. The rules for negation are therefore similar to the rules for implication. To prove ¬ A, assume A and derive a contradiction.

section
  example : ¬ A :=
  assume h : A,
  show false, from sorry
end
section
  example : ¬ A :=
  assume h : A,
  show false, from sorry
end

如果你已经证明了一个否定,那么你可以将其应用到的证明中从而得到矛盾。¬ AA

If you have proved a negation ¬ A, you can get a contradiction by applying it to a proof of A.

section
  variable h1 : ¬ A
  variable h2 : A

  example : false := h1 h2
end
section
  variable h1 : ¬ A
  variable h2 : A

  example : false := h1 h2
end

4.3.5.真与假

4.3.5. Truth and falsity

反面的规则称为false.elim

The ex falso rule is called false.elim:

section
  variable h : false

  example : A := false.elim h
end
section
  variable h : false

  example : A := false.elim h
end

true除了这是显而易见的事实之外,没有什么可说的:

There isn’t much to say about true beyond the fact that it is trivially true:

example : true := trivial
example : true := trivial

4.3.6.双向蕴涵

4.3.6. Bi-implication

“当且仅当”的引入规则是iff.intro

The introduction rule for “if and only if” is iff.intro.

example : A  B :=
iff.intro
  (assume h : A,
    show B, from sorry)
  (assume h : B,
    show A, from sorry)
example : A  B :=
iff.intro
  (assume h : A,
    show B, from sorry)
  (assume h : B,
    show A, from sorry)

和往常一样,我们选择缩进以使结构清晰。请注意,同一个标签h可以在两个分支上使用,每个分支的含义不同,因为 an 的范围assume仅限于它出现的表达式。

As usual, we have chosen indentation to make the structure clear. Notice that the same label, h, can be used on both branches, with a different meaning in each, because the scope of an assume is limited to the expression in which it appears.

消除规则是iff.elim_leftiff.elim_right

The elimination rules are iff.elim_left and iff.elim_right:

section
  variable h1 : A  B
  variable h2 : A

  example : B := iff.elim_left h1 h2
end

section
  variable h1 : A  B
  variable h2 : B

  example : A := iff.elim_right h1 h2
end
section
  variable h1 : A  B
  variable h2 : A

  example : B := iff.elim_left h1 h2
end

section
  variable h1 : A  B
  variable h2 : B

  example : A := iff.elim_right h1 h2
end

iff.mpLean 识别的缩写iff.elim_left,其中“mp”代表“肯定前件”。同样,您可以使用iff.mpr代替 来表示“肯定前件反向” iff.elim_right

Lean recognizes the abbreviation iff.mp for iff.elim_left, where “mp” stands for “modus ponens”. Similarly, you can use iff.mpr, for “modus ponens reverse”, instead of iff.elim_right.

4.3.7.归谬法 (反证法证明)

4.3.7. Reductio ad absurdum (proof by contradiction)

最后,还有反证法规则,我们将在第 5 章中详细讨论。这里将其纳入其中是为了完整性。

Finally, there is the rule for proof by contradiction, which we will discuss in greater detail in Chapter 5. It is included for completeness here.

该规则称为。它有一个参数,即来自by_contradiction的证明。要使用该规则,您必须要求 Lean 允许经典推理,方法是编写。您可以在文件开头或使用它之前的任何时间执行此操作。如果您在某个部分中说,它将保留在该部分的范围中。false¬ Aopen classicalopen classical

The rule is called by_contradiction. It has one argument, which is a proof of false from ¬ A. To use the rule, you have to ask Lean to allow classical reasoning, by writing open classical. You can do this at the beginning of the file, or any time before using it. If you say open classical in a section, it will remain in scope for that section.

section
  open classical

  example : A :=
  by_contradiction
    (assume h : ¬ A,
      show false, from sorry)
end
section
  open classical

  example : A :=
  by_contradiction
    (assume h : ¬ A,
      show false, from sorry)
end

4.3.8.示例

4.3.8. Examples

在上一章中,我们构造了以下证明ACABBC

In the last chapter, we constructed the following proof of AC from AB and BC:

我们可以在精益中对此进行如下建模:

We can model this in Lean as follows:

variables A B C : Prop

variable h1 : A  B
variable h2 : B  C

example : A  C :=
assume h : A,
show C, from h2 (h1 h)
variables A B C : Prop

variable h1 : A  B
variable h2 : B  C

example : A  C :=
assume h : A,
show C, from h2 (h1 h)

请注意,自然演绎证明中未被取消的假设在 Lean 版本中被声明为变量。

Notice that the hypotheses in the natural deduction proof that are not canceled are declared as variables in the Lean version.

我们还构造了以下证明:

We also constructed the following proof:

在 Lean 中它的写法如下:

Here is how it is written in Lean:

example (A B C : Prop) : (A  (B  C))  (A  B  C) :=
assume h1 : A  (B  C),
assume h2 : A  B,
show C, from h1 (and.left h2) (and.right h2)
example (A B C : Prop) : (A  (B  C))  (A  B  C) :=
assume h1 : A  (B  C),
assume h2 : A  B,
show C, from h1 (and.left h2) (and.right h2)

这是可行的,因为是的证明,并且是的证明。and.left h2Aand.right h2B

This works because and.left h2 is a proof of A, and and.right h2 is a proof of B.

最后,我们构造了以下证明A(BC)(AB)(AC)

Finally, we constructed the following proof of A(BC)(AB)(AC):

以下是 Lean 的一个版本:

Here is a version in Lean:

example (A B C : Prop) : A  (B  C)  (A  B)  (A  C) :=
assume h1 : A  (B  C),
or.elim (and.right h1)
  (assume h2 : B,
    show (A  B)  (A  C),
      from or.inl (and.intro (and.left h1) h2))
  (assume h2 : C,
    show (A  B)  (A  C),
      from or.inr (and.intro (and.left h1) h2))
example (A B C : Prop) : A  (B  C)  (A  B)  (A  C) :=
assume h1 : A  (B  C),
or.elim (and.right h1)
  (assume h2 : B,
    show (A  B)  (A  C),
      from or.inl (and.intro (and.left h1) h2))
  (assume h2 : C,
    show (A  B)  (A  C),
      from or.inr (and.intro (and.left h1) h2))

事实上,考虑到assume是符号的替代语法λ,并且 Lean 通常可以推断出假设的类型,我们可以使证明非常简短:

In fact, bearing in mind that assume is alternative syntax for the symbol λ, and that Lean can often infer the type of an assumption, we can make the proof remarkably brief:

example (A B C : Prop) : A  (B  C)  (A  B)  (A  C) :=
λ h1, or.elim (and.right h1)
  (λ h2, or.inl (and.intro (and.left h1) h2))
  (λ h2, or.inr (and.intro (and.left h1) h2))
example (A B C : Prop) : A  (B  C)  (A  B)  (A  C) :=
λ h1, or.elim (and.right h1)
  (λ h2, or.inl (and.intro (and.left h1) h2))
  (λ h2, or.inr (and.intro (and.left h1) h2))

不过,证明是隐晦的。使用这样的风格使得证明难以编写、阅读、理解、维护和调试。在下一节中,我们将描述一种非常简单的设备,它使人们更容易理解正在发生的事情。

The proof is cryptic, though. Using such a style makes proofs hard to write, read, understand, maintain, and debug. In the next section we will describe a remarkably simple device that makes it much easier to understand what is going on.

4.4.正向推理

4.4. Forward Reasoning

Lean 允许您使用命令编写证明,从而支持正向推理have

Lean supports forward reasoning by allowing you to write proofs using the have command.

variables A B C : Prop

variable h1 : A  B
variable h2 : B  C

example : A  C :=
assume h : A,
have h3 : B, from h1 h,
show C, from h2 h3
variables A B C : Prop

variable h1 : A  B
variable h2 : B  C

example : A  C :=
assume h : A,
have h3 : B, from h1 h,
show C, from h2 h3

用 写出证明与写出 的效果相同。此命令检查是 的证明,然后给出 用来代替 的标签。因此,可以将上一个证明的最后一行看作是 的缩写,因为缩写。这种缩写会产生很大的不同,尤其是在证明很长的时候。have h : A, from P, ... h ...... P ...havePAhPshow C, from h2 (h1 h)h3h1 hP

Writing a proof with have h : A, from P, ... h ... has the same effect as writing ... P .... This have command checks that P is a proof of A, and then give you the label h to use in place of P. Thus the last line of the previous proof can be thought of as abbreviating show C, from h2 (h1 h), since h3 abbreviates h1 h. Such abbreviations can make a big difference, especially when the proof P is very long.

使用 有很多好处have。首先,它使证明更具可读性:上面的例子B明确地说明了辅助目标。它还可以节省重复:h3引入后可以重复使用,而无需重复证明。最后,它使构造和调试证明变得更容易:B作为辅助目标陈述使精益更容易在目标未正确实现时提供信息性错误消息。

There are a number of advantages to using have. For one thing, it makes the proof more readable: the example above states B explicitly as an auxiliary goal. It can also save repetition: h3 can be used repeatedly after it is introduced, without duplicating the proof. Finally, it makes it easier to construct and debug the proof: stating B as an auxiliary goal makes it easier for Lean to deliver an informative error message when the goal is not properly met.

在上一节中,我们考虑了以下证明:

In the last section, we considered the following proof:

example (A B C : Prop) : (A  (B  C))  (A  B  C) :=
assume h1 : A  (B  C),
assume h2 : A  B,
show C, from h1 (and.left h2) (and.right h2)
example (A B C : Prop) : (A  (B  C))  (A  B  C) :=
assume h1 : A  (B  C),
assume h2 : A  B,
show C, from h1 (and.left h2) (and.right h2)

使用have,可以更清晰地写成如下形式:

Using have, it can be written more perspicuously as follows:

example (A B C : Prop) : (A  (B  C))  (A  B  C) :=
assume h1 : A  (B  C),
assume h2 : A  B,
have h3 : A, from and.left h2,
have h4 : B, from and.right h2,
show C, from h1 h3 h4
example (A B C : Prop) : (A  (B  C))  (A  B  C) :=
assume h1 : A  (B  C),
assume h2 : A  B,
have h3 : A, from and.left h2,
have h4 : B, from and.right h2,
show C, from h1 h3 h4

我们还可以更加详细,添加另一行:

We can be even more verbose, and add another line:

example (A B C : Prop) : (A  (B  C))  (A  B  C) :=
assume h1 : A  (B  C),
assume h2 : A  B,
have h3 : A, from and.left h2,
have h4 : B, from and.right h2,
have h5 : B  C, from h1 h3,
show C, from h5 h4
example (A B C : Prop) : (A  (B  C))  (A  B  C) :=
assume h1 : A  (B  C),
assume h2 : A  B,
have h3 : A, from and.left h2,
have h4 : B, from and.right h2,
have h5 : B  C, from h1 h3,
show C, from h5 h4

添加更多信息并不总是能使证明更具可读性;当单个表达式很小且足够容易理解时,详细地拼写它们可能会造成混乱。在学习使用精益时,您必须发展自己的风格,并运用您的判断力来决定哪些步骤需要明确。

Adding more information doesn’t always make a proof more readable; when the individual expressions are small and easy enough to understand, spelling them out in detail can introduce clutter. As you learn to use Lean, you will have to develop your own style, and use your judgment to decide which steps to make explicit.

以下是一些基本推论在用 展开后的样子have。在 and-introduction 规则中,首先要显示每个合取项,然后将它们放在一起:

Here is how some of the basic inferences look, when expanded with have. In the and-introduction rule, it is a matter showing each conjunct first, and then putting them together:

example (A B : Prop) : A  B  B  A :=
assume h1 : A  B,
have h2 : A, from and.left h1,
have h3 : B, from and.right h1,
show B  A, from and.intro h3 h2
example (A B : Prop) : A  B  B  A :=
assume h1 : A  B,
have h2 : A, from and.left h1,
have h3 : B, from and.right h1,
show B  A, from and.intro h3 h2

与此版本相比,此版本首先说明我们将使用该and.intro规则,然后明确提出两个最终目标:

Compare that with this version, which instead states first that we will use the and.intro rule, and then makes the two resulting goals explicit:

example (A B : Prop) : A  B  B  A :=
assume h1 : A  B,
show B  A, from
  and.intro
    (show B, from and.right h1)
    (show A, from and.left h1)
example (A B : Prop) : A  B  B  A :=
assume h1 : A  B,
show B  A, from
  and.intro
    (show B, from and.right h1)
    (show A, from and.left h1)

再次强调,问题只在于可读性。Lean 用以下简短版本就做得很好:

Once again, at issue is only readability. Lean does just fine with the following short version:

example (A B : Prop) : A  B  B  A :=
λ h, and.intro (and.right h) (and.left h)
example (A B : Prop) : A  B  B  A :=
λ h, and.intro (and.right h) (and.left h)

使用或消除规则时,最清楚的做法通常是明确说明相关的析取:

When using the or-elimination rule, it is often clearest to state the relevant disjunction explicitly:

example (A B C : Prop) : C :=
have h : A  B, from sorry,
show C, from or.elim h
  (assume h1 : A,
    show C, from sorry)
  (assume h2 : B,
    show C, from sorry)
example (A B C : Prop) : C :=
have h : A  B, from sorry,
show C, from or.elim h
  (assume h1 : A,
    show C, from sorry)
  (assume h2 : B,
    show C, from sorry)

以下是have上一节中示例的结构化呈现:

Here is a have-structured presentation of an example from the previous section:

example (A B C : Prop) : A  (B  C)  (A  B)  (A  C) :=
assume h1 : A  (B  C),
have h2 : A, from and.left h1,
have h3 : B  C, from and.right h1,
show (A  B)  (A  C), from
  or.elim h3
    (assume h4 : B,
      have h5 : A  B, from and.intro h2 h4,
      show (A  B)  (A  C), from or.inl h5)
    (assume h4 : C,
      have h5 : A  C, from and.intro h2 h4,
      show (A  B)  (A  C), from or.inr h5)
example (A B C : Prop) : A  (B  C)  (A  B)  (A  C) :=
assume h1 : A  (B  C),
have h2 : A, from and.left h1,
have h3 : B  C, from and.right h1,
show (A  B)  (A  C), from
  or.elim h3
    (assume h4 : B,
      have h5 : A  B, from and.intro h2 h4,
      show (A  B)  (A  C), from or.inl h5)
    (assume h4 : C,
      have h5 : A  C, from and.intro h2 h4,
      show (A  B)  (A  C), from or.inr h5)

4.5.定义和定理

4.5. Definitions and Theorems

精益允许我们命名定义和定理以供以后使用。例如,这是新“连接词”的定义:

Lean allows us to name definitions and theorems for later use. For example, here is a definition of a new “connective”:

def triple_and (A B C : Prop) : Prop :=
A  (B  C)
def triple_and (A B C : Prop) : Prop :=
A  (B  C)

与命令一样,参数、和是否事先用命令声明,或用定义本身声明example并不重要。然后我们可以将定义应用于任何表达式:ABCvariables

As with the example command, it does not matter whether the arguments A, B, and C are declared beforehand with the variables command, or with the definition itself. We can then apply the definition to any expressions:

variables D E F G : Prop

#check triple_and (D  E) (¬ F  G) (¬ D)
variables D E F G : Prop

#check triple_and (D  E) (¬ F  G) (¬ D)

后面我们还会看到更多有趣的定义的例子,像下面这个从自然数到自然数的函数,它的输入加倍了:

Later, we will see more interesting examples of definitions, like the following function from natural numbers to natural numbers, which doubles its input:

def double (n : ) :  := n + n
def double (n : ) :  := n + n

现在更有趣的是,Lean 还允许我们命名定理,并在以后将其用作推理规则。例如,考虑以下定理:

What is more interesting right now is that Lean also allows us to name theorems, and use them later, as rules of inference. For example, consider the following theorem:

theorem and_commute (A B : Prop) : A  B  B  A :=
assume h, and.intro (and.right h) (and.left h)
theorem and_commute (A B : Prop) : A  B  B  A :=
assume h, and.intro (and.right h) (and.left h)

一旦我们定义了它,我们就可以自由地使用它:

Once we have defined it, we can use it freely:

variables C D E : Prop
variable h1 : C  ¬ D
variable h2 : ¬ D  C  E

example : E := h2 (and_commute C (¬ D) h1)
variables C D E : Prop
variable h1 : C  ¬ D
variable h2 : ¬ D  C  E

example : E := h2 (and_commute C (¬ D) h1)

在这个例子中,我们必须明确给出参数C和,因为它们隐含在中,这很烦人。事实上,Lean 允许我们在 的定义中告诉 Lean 这一点:¬ Dh1and_commute

It is annoying in this example that we have to give the arguments C and ¬ D explicitly, because they are implicit in h1. In fact, Lean allows us to tell this to Lean in the definition of and_commute:

theorem and_commute {A B : Prop} : A  B  B  A :=
assume h, and.intro (and.right h) (and.left h)
theorem and_commute {A B : Prop} : A  B  B  A :=
assume h, and.intro (and.right h) (and.left h)

这里的花括号表示参数AB隐式的,也就是说,当使用定理时,Lean 应该从上下文中推断出它们。然后我们可以改写以下内容:

Here the squiggly braces indicate that the arguments A and B are implicit, which is to say, Lean should infer them from the context when the theorem is used. We can then write the following instead:

variables C D E : Prop
variable h1 : C  ¬ D
variable h2 : ¬ D  C  E

example : E := h2 (and_commute h1)
variables C D E : Prop
variable h1 : C  ¬ D
variable h2 : ¬ D  C  E

example : E := h2 (and_commute h1)

事实上,Lean 的库中有一个定理,and_comm其定义正是如此。

Indeed, Lean’s library has a theorem, and_comm, defined in exactly this way.

顺便说一下,我们可以通过将假设变成论点来避免assume证明中的步骤:and_comm

By the way, we could avoid the assume step in the proof of and_comm by making the hypothesis into an argument:

theorem and_commute {A B : Prop} (h : A  B) : B  A :=
and.intro (and.right h) (and.left h)
theorem and_commute {A B : Prop} (h : A  B) : B  A :=
and.intro (and.right h) (and.left h)

这两个定义产生相同的结果。

The two definitions yield the same result.

定义和定理在数学中很重要;它们使我们能够从基本原理构建复杂的理论。精益也接受用这个词lemma来代替theorem

Definitions and theorems are important in mathematics; they allow us to build up complex theories from fundamental principles. Lean also accepts the word lemma instead of theorem.

有趣的是,在交互式定理证明中,我们甚至可以定义熟悉的推理模式。例如,上一章中提到了以下所有推理:

What is interesting is that in interactive theorem proving, we can even define familiar patterns of inference. For example, all of the following inferences were mentioned in the last chapter:

namespace hidden

variables {A B : Prop}

theorem or_resolve_left (h1 : A  B) (h2 : ¬ A) : B :=
or.elim h1
  (assume h3 : A, show B, from false.elim (h2 h3))
  (assume h3 : B, show B, from h3)

theorem or_resolve_right (h1 : A  B) (h2 : ¬ B) : A :=
or.elim h1
  (assume h3 : A, show A, from h3)
  (assume h3 : B, show A, from false.elim (h2 h3))

theorem absurd (h1 : ¬ A) (h2 : A) : B :=
false.elim (h1 h2)

end hidden
namespace hidden

variables {A B : Prop}

theorem or_resolve_left (h1 : A  B) (h2 : ¬ A) : B :=
or.elim h1
  (assume h3 : A, show B, from false.elim (h2 h3))
  (assume h3 : B, show B, from h3)

theorem or_resolve_right (h1 : A  B) (h2 : ¬ B) : A :=
or.elim h1
  (assume h3 : A, show A, from h3)
  (assume h3 : B, show A, from false.elim (h2 h3))

theorem absurd (h1 : ¬ A) (h2 : A) : B :=
false.elim (h1 h2)

end hidden

事实上,Lean 的库定义了or.resolve_leftor.resolve_rightabsurd。我们使用namespace命令来避免命名冲突,否则会引发错误。

In fact, Lean’s library defines or.resolve_left, or.resolve_right, and absurd. We used the namespace command to avoid naming conflicts, which would have raised an error.

当我们要求您用 Lean 中的命题逻辑证明基本事实时,与命题逻辑一样,我们的目标是让您学会如何使用 Lean 的原语。因此,对于这些练习,您不应该使用库中的事实。然而,随着我们转向真正的数学,您可以更自由地使用库中的事实。

When we ask you to prove basic facts from propositional logic in Lean, as with propositional logic, our goal is to have you learn how to use Lean’s primitives. As a result, for those exercises, you should not use facts from the library. As we move towards real mathematics, however, you can use facts from the library more freely.

4.6.附加语法

4.6. Additional Syntax

在本节中,我们为高级用户描述了 Lean 的一些额外语法功能。语法小工具通常很方便,有时可以使证明看起来更漂亮。

In this section, we describe some extra syntactic features of Lean, for power users. The syntactic gadgets are often convenient, and sometimes make proofs look prettier.

首先,您可以使用带反斜杠的下标数字。例如,您可以输入h₁h\1标签与 Lean 无关,因此差异只是表面上的。

For one thing, you can use subscripted numbers with a backslash. For example, you can write h₁ by typing h\1. The labels are irrelevant to Lean, so the difference is only cosmetic.

另一个特点是,你可以在语句中省略标签assume,从而提供“匿名”假设。然后,你可以使用关键字引用最后一个匿名假设this

Another feature is that you can omit the label in an assume statement, providing an “anonymous” hypothesis. You can then refer back to the last anonymous assumption using the keyword this:

example : A  A  B :=
assume : A,
show A  B, from or.inl this
example : A  A  B :=
assume : A,
show A  B, from or.inl this

或者,您可以通过将未标记的假设放在法语引号中来引用它们:

Alternatively, you can refer back to unlabeled assumptions by putting them in French quotes:

example : A  B  A  B :=
assume : A,
assume : B,
show A  B, from and.intro A B
example : A  B  A  B :=
assume : A,
assume : B,
show A  B, from and.intro A B

您也可以使用该词have而不给出标签,然后使用相同的惯例引用它们。以下是使用这些功能的示例:

You can also use the word have without giving a label, and refer back to them using the same conventions. Here is an example that uses these features:

theorem my_theorem {A B C : Prop} :
  A  (B  C)  (A  B)  (A  C) :=
assume h : A  (B  C),
have A, from and.left h,
have B  C, from and.right h,
show (A  B)  (A  C), from
  or.elim B  C
    (assume : B,
      have A  B, from and.intro A B›,
      show (A  B)  (A  C), from or.inl this)
    (assume : C,
      have A  C, from and.intro A C›,
      show (A  B)  (A  C), from or.inr this)
theorem my_theorem {A B C : Prop} :
  A  (B  C)  (A  B)  (A  C) :=
assume h : A  (B  C),
have A, from and.left h,
have B  C, from and.right h,
show (A  B)  (A  C), from
  or.elim B  C
    (assume : B,
      have A  B, from and.intro A B›,
      show (A  B)  (A  C), from or.inl this)
    (assume : C,
      have A  C, from and.intro A C›,
      show (A  B)  (A  C), from or.inr this)

另一个技巧是,你可以用h.leftandh.right代替and每当是连词,也可以用(使用and ) 代替每当 Lean 能确定你要证明的是连词时。使用这些约定,你可以编写以下内容:and.left hand.right hh⟨h1, h2⟩\<\>and.intro h1 h2

Another trick is that you can write h.left and h.right instead of and.left h and and.right h whenever h is a conjunction, and you can write ⟨h1, h2⟩ (using \< and \>) instead of and.intro h1 h2 whenever Lean can figure out that a conjunction is what you are trying to prove. With these conventions, you can write the following:

example (A B : Prop) : A  B  B  A :=
assume h : A  B,
show B  A, from h.right, h.left
example (A B : Prop) : A  B  B  A :=
assume h : A  B,
show B  A, from h.right, h.left

这只不过是下列内容的简写:

This is nothing more than shorthand for the following:

example (A B : Prop) : A  B  B  A :=
assume h : A  B,
show B  A, from and.intro (and.right h) (and.left h)
example (A B : Prop) : A  B  B  A :=
assume h : A  B,
show B  A, from and.intro (and.right h) (and.left h)

更简洁地说,你可以这样写:

Even more concisely, you can write this:

example (A B : Prop) : A  B  B  A :=
assume h, h.right, h.left
example (A B : Prop) : A  B  B  A :=
assume h, h.right, h.left

你甚至可以将连词与 分开assume,以便可以这样工作:

You can even take apart a conjunction with an assume, so that this works:

example (A B : Prop) : A  B  B  A :=
assume h₁, h₂⟩, h₂, h₁
example (A B : Prop) : A  B  B  A :=
assume h₁, h₂⟩, h₂, h₁

类似地,如果h是双条件,则可以用h.mph.mpr代替和,也可以用代替。因此,Lean 理解这些证明:iff.mp hiff.mpr h⟨h1, h2⟩iff.intro h1 h2

Similarly, if h is a biconditional, you can write h.mp and h.mpr instead of iff.mp h and iff.mpr h, and you can write ⟨h1, h2⟩ instead of iff.intro h1 h2. As a result, Lean understands these proofs:

example (A B : Prop) : B  (A  B)  A :=
assume hB, hAB⟩,
hAB.mpr hB

example (A B : Prop) : A  B  B  A :=
assume h₁, h₂⟩, h₂, h₁⟩, assume h₁, h₂⟩, h₂, h₁⟩⟩
example (A B : Prop) : B  (A  B)  A :=
assume hB, hAB⟩,
hAB.mpr hB

example (A B : Prop) : A  B  B  A :=
assume h₁, h₂⟩, h₂, h₁⟩, assume h₁, h₂⟩, h₂, h₁⟩⟩

最后,您可以通过两种方式向证明添加注释。首先,--Lean 处理器会忽略双破折号之后直到行尾的任何文本。其次,/-和之间的任何文本-/表示块注释,也会被忽略。您可以嵌套块注释。

Finally, you can add comments to your proofs in two ways. First, any text after a double-dash -- until the end of a line is ignored by the Lean processor. Second, any text between /- and -/ denotes a block comment, and is also ignored. You can nest block comments.

/- This is a block comment.
   It can fill multiple lines. -/

example (A : Prop) : A  A :=
assume : A,        -- assume the antecedent
show A, from this  -- use it to establish the conclusion
/- This is a block comment.
   It can fill multiple lines. -/

example (A : Prop) : A  A :=
assume : A,        -- assume the antecedent
show A, from this  -- use it to establish the conclusion

4.7.练习

4.7. Exercises

在精益中证明以下内容:

Prove the following in Lean:

variables A B C D : Prop

example : A  (A  B)  B :=
sorry

example : A  ¬ (¬ A  B) :=
sorry

example : ¬ (A  B)  (A  ¬ B) :=
sorry

example (h₁ : A  B) (h₂ : A  C) (h₃ : B  D) : C  D :=
sorry

example (h : ¬ A  ¬ B) : ¬ (A  B) :=
sorry

example : ¬ (A  ¬ A) :=
sorry
variables A B C D : Prop

example : A  (A  B)  B :=
sorry

example : A  ¬ (¬ A  B) :=
sorry

example : ¬ (A  B)  (A  ¬ B) :=
sorry

example (h₁ : A  B) (h₂ : A  C) (h₃ : B  D) : C  D :=
sorry

example (h : ¬ A  ¬ B) : ¬ (A  B) :=
sorry

example : ¬ (A  ¬ A) :=
sorry

5.经典推理

5. Classical Reasoning

如果我们采用迄今为止所见的所有命题逻辑规则,并排除归谬法或反证法,我们就得到了所谓的直觉逻辑。在直觉逻辑中,可以从计算的角度来看待证明:AB证明A并附有证明B,证明AB是一个将证据转化为A成为证据B,以及AB是其中一个的证明,并加了标签,以便我们知道是哪种情况。ex falso规则之所以有意义,只是因为我们预计没有虚假性的证据;它就像空数据类型。

If we take all the rules of propositional logic we have seen so far and exclude reductio ad absurdum, or proof by contradiction, we have what is known as intuitionistic logic. In intuitionistic logic, it is possible to view proofs in computational terms: a proof of AB is a proof of A paired with a proof of B, a proof of AB is a procedure which transforms evidence for A into evidence for B, and a proof of AB is a proof of one or the other, tagged so that we know which is the case. The ex falso rule makes sense only because we expect that there is no proof of falsity; it is like the empty data type.

矛盾证明与这种世界观不太相符:从矛盾证明¬A,我们应该神奇地证明A我们将看到,通过反证法,我们可以证明以下定律,即排中律A,A¬A从计算的角度来看,这意味着对于每一个A我们可以决定是否A是真的。

Proof by contradiction does not fit in well with this world view: from a proof of a contradiction from ¬A, we are supposed to magically produce a proof of A. We will see that with proof by contradiction, we can prove the following law, known as the law of the excluded middle: A,A¬A. From a computational perspective, this says that for every A we can decide whether or not A is true.

然而,经典推理确实在逻辑中引入了许多原则,这些原则可用于简化推理。在本章中,我们将考虑这些原则,并看看它们如何遵循基本规则。

Classical reasoning does introduce a number of principles into logic, however, that can be used to simplify reasoning. In this chapter, we will consider these principles, and see how they follow from the basic rules.

5.1.矛盾证明

5.1. Proof by Contradiction

请记住,在自然演绎中,矛盾证明用以下模式表达:

Remember that in natural deduction, proof by contradiction is expressed by the following pattern:

假设¬A在最终推理时被取消。

The assumption ¬A is canceled at the final inference.

在精益中,推理被称为by_contradiction,由于它是经典规则,我们必须先使用 命令,它才可用。一旦我们这样做,推理模式表达如下:open classical

In Lean, the inference is named by_contradiction, and since it is a classical rule, we have to use the command open classical before it is available. Once we do so, the pattern of inference is expressed as follows:

open classical

variable (A : Prop)

example : A :=
by_contradiction
  (assume h : ¬ A,
    show false, from sorry)
open classical

variable (A : Prop)

example : A :=
by_contradiction
  (assume h : ¬ A,
    show false, from sorry)

该规则的一个最重要的推论就是我们上面提到的经典原理,即排中律,它断言,对于所有AA¬A在 Lean 中,我们用 表示该定律em。在数学论证中,人们经常将证明分为两种情况,首先假设A进而¬A. 使用消去规则进行析取,这相当于使用A¬A,这是针对这一特定问题的排中原理A

One of the most important consequences of this rule is a classical principle that we mentioned above, namely, the law of the excluded middle, which asserts that the following holds for all A: A¬A. In Lean we denote this law by em. In mathematical arguments, one often splits a proof into two cases, assuming first A and then ¬A. Using the elimination rule for disjunction, this is equivalent to using A¬A, which is the excluded middle principle for this particular A.

以下是自然演绎中的 的证明em,使用矛盾证明:

Here is a proof of em, in natural deduction, using proof by contradiction:

以下是在 Lean 中呈现的相同证明:

Here is the same proof rendered in Lean:

open classical

variable (A : Prop)

example : A  ¬ A :=
by_contradiction
  (assume h1 : ¬ (A  ¬ A),
    have h2 : ¬ A, from
      assume h3 : A,
      have h4 : A  ¬ A, from or.inl h3,
      show false, from h1 h4,
    have h5 : A  ¬ A, from or.inr h2,
    show false, from h1 h5)
open classical

variable (A : Prop)

example : A  ¬ A :=
by_contradiction
  (assume h1 : ¬ (A  ¬ A),
    have h2 : ¬ A, from
      assume h3 : A,
      have h4 : A  ¬ A, from or.inl h3,
      show false, from h1 h4,
    have h5 : A  ¬ A, from or.inr h2,
    show false, from h1 h5)

该原理被称为排中律,因为它表明一个命题A要么为真要么为假;没有中间立场。因此,该定理em在 Lean 库中被命名。对于任何命题A,表示 的证明,您可以在任何时候使用它:em AA ¬ Aclassical

The principle is known as the law of the excluded middle because it says that a proposition A is either true or false; there is no middle ground. As a result, the theorem is named em in the Lean library. For any proposition A, em A denotes a proof of A ¬ A, and you are free to use it any time classical is open:

open classical

example (A : Prop) : A  ¬ A :=
or.elim (em A)
  (assume : A, or.inl this)
  (assume : ¬ A, or.inr this)
open classical

example (A : Prop) : A  ¬ A :=
or.elim (em A)
  (assume : A, or.inl this)
  (assume : ¬ A, or.inr this)

或者更简单一点:

Or even more simply:

open classical

example (A : Prop) : A  ¬ A :=
em A
open classical

example (A : Prop) : A  ¬ A :=
em A

事实上,我们可以反过来,使用排中律来证明反证法。练习中要求你这样做。

In fact, we can go in the other direction, and use the law of the excluded middle to justify proof by contradiction. You are asked to do this in the exercises.

反证法也等同于原理¬¬AA。从右到左的蕴涵在直觉上成立;另一个蕴涵是经典的,被称为双重否定消去。以下是自然演绎的证明:

Proof by contradiction is also equivalent to the principle ¬¬AA. The implication from right to left holds intuitionistically; the other implication is classical, and is known as double-negation elimination. Here is a proof in natural deduction:

以下是 Lean 中的相应证明:

And here is the corresponding proof in Lean:

open classical

example (A : Prop) : ¬ ¬ A  A :=
iff.intro
  (assume h1 : ¬ ¬ A,
    show A, from by_contradiction
      (assume h2 : ¬ A,
        show false, from h1 h2))
  (assume h1 : A,
    show ¬ ¬ A, from assume h2 : ¬ A, h2 h1)
open classical

example (A : Prop) : ¬ ¬ A  A :=
iff.intro
  (assume h1 : ¬ ¬ A,
    show A, from by_contradiction
      (assume h2 : ¬ A,
        show false, from h1 h2))
  (assume h1 : A,
    show ¬ ¬ A, from assume h2 : ¬ A, h2 h1)

在下一节中,我们将推导一些经典规则和等价关系。这些规则和等价关系很难证明。一般来说,要在自然演绎中使用经典推理,我们需要扩展第 3.3 节中提出的一般启发式方法,如下所示:

In the next section, we will derive a number of classical rules and equivalences. These are tricky to prove. In general, to use classical reasoning in natural deduction, we need to extend the general heuristic presented in Section 3.3 as follows:

  1. 首先,利用引言规则,从结论开始逆向推导。

  2. First, work backward from the conclusion, using the introduction rules.

  3. 当第一步没有事情要做的时候,就用消除规则来继续进行。

  4. When you have run out things to do in the first step, use elimination rules to work forward.

  5. 如果其他方法都失败了,就使用反证法。

  6. If all else fails, use a proof by contradiction.

有时反证法是必要的,但当它不是时,它可能比直接证明更不具信息量。例如,假设我们想要证明ABCD。在直接证明中,我们假设AB, 和C并努力实现D. 在此过程中,我们将得出AB, 和C,这些在其他情况下可能有用。另一方面,如果我们使用反证法,我们假设ABC, 和¬D并尝试证明。在这种情况下,我们在一个不一致的背景下工作;我们通过这种方式获得的任何辅助结果都被归结为最终是假设的结果。

Sometimes a proof by contradiction is necessary, but when it isn’t, it can be less informative than a direct proof. Suppose, for example, we want to prove ABCD. In a direct proof, we assume A, B, and C, and work towards D. Along the way, we will derive other consequences of A, B, and C, and these may be useful in other contexts. If we use proof by contradiction, on the other hand, we assume A, B, C, and ¬D, and try to prove . In that case, we are working in an inconsistent context; any auxiliary results we may obtain that way are subsumed by the fact that ultimately is a consequence of the hypotheses.

5.2.一些经典原则

5.2. Some Classical Principles

我们已经看到A¬A¬¬AA是经典命题逻辑的两个重要定理。在本节中,我们将提供更多定理、规则和等价关系。其中一些将在这里证明,但大多数将留给您在练习中证明。在普通数学中,这些通常不加注释地使用。然而,很高兴知道,它们都可以使用经典自然演绎的基本规则来证明。

We have already seen that A¬A and ¬¬AA are two important theorems of classical propositional logic. In this section we will provide some more theorems, rules, and equivalences. Some will be proved here, but most will be left to you in the exercises. In ordinary mathematics, these are generally used without comment. It is nice to know, however, that they can all be justified using the basic rules of classical natural deduction.

如果AB是任何蕴涵,断言¬B¬A被称为逆否命题。每个蕴涵式都蕴涵其逆否命题,而另一个方向在经典上为真:

If AB is any implication, the assertion ¬B¬A is known as the contrapositive. Every implication implies its contrapositive, and the other direction is true classically:

这是另一个例子。直观地说,断言“如果 A 则 B”相当于说 A 为真而 B 为假的情况不可能存在。我们需要经典推理才能从第二个陈述得出第一个陈述。

Here is another example. Intuitively, asserting “if A then B” is equivalent to saying that it cannot be the case that A is true and B is false. Classical reasoning is needed to get us from the second statement to the first.

以下是用 Lean 呈现的相同证明:

Here are the same proofs, rendered in Lean:

open classical

variables (A B : Prop)

example (h : ¬ B  ¬ A) : A  B :=
assume h1 : A,
show B, from
  by_contradiction
    (assume h2 : ¬ B,
      have h3 : ¬ A, from h h2,
      show false, from h3 h1)

example (h : ¬ (A  ¬ B)) : A  B :=
assume : A,
show B, from
  by_contradiction
    (assume : ¬ B,
      have A  ¬ B, from and.intro A this,
      show false, from h this)
open classical

variables (A B : Prop)

example (h : ¬ B  ¬ A) : A  B :=
assume h1 : A,
show B, from
  by_contradiction
    (assume h2 : ¬ B,
      have h3 : ¬ A, from h h2,
      show false, from h3 h1)

example (h : ¬ (A  ¬ B)) : A  B :=
assume : A,
show B, from
  by_contradiction
    (assume : ¬ B,
      have A  ¬ B, from and.intro A this,
      show false, from h this)

请注意,在第二个例子中,我们使用了匿名assume和匿名have。我们使用括号\f<\f>来写‹A›,以引用第一个假设。该词的第一次使用this引用了假设,而第二次使用则引用了。¬ Bhave

Notice that in the second example, we used an anonymous assume and an anonymous have. We used the brackets \f< and \f> to write ‹A›, referring back to the first assumption. The first use of the word this refers back to the assumption ¬ B, while the second one refers back to the have.

既然我们可以证明排中律,那么在经典证明中使用它就很方便了。下面是一个例子,其中证明了(AB)(BA)

Knowing that we can prove the law of the excluded middle, it is convenient to use it in classical proofs. Here is an example, with a proof of (AB)(BA):

以下是 Lean 中相应的证明:

Here is the corresponding proof in Lean:

open classical

variables (A B : Prop)

example : (A  B)  (B  A) :=
or.elim (em B)
  (assume h : B,
    have A  B, from
      assume : A,
      show B, from h,
    show (A  B)  (B  A),
      from or.inl this)
  (assume h : ¬ B,
    have B  A, from
      assume : B,
      have false, from h this,
      show A, from false.elim this,
    show (A  B)  (B  A),
      from or.inr this)
open classical

variables (A B : Prop)

example : (A  B)  (B  A) :=
or.elim (em B)
  (assume h : B,
    have A  B, from
      assume : A,
      show B, from h,
    show (A  B)  (B  A),
      from or.inl this)
  (assume h : ¬ B,
    have B  A, from
      assume : B,
      have false, from h this,
      show A, from false.elim this,
    show (A  B)  (B  A),
      from or.inr this)

使用经典推理,蕴涵可以用析取和否定的形式重写:

Using classical reasoning, implication can be rewritten in terms of disjunction and negation:

(AB)¬AB.

前进的方向需要经典推理。

The forward direction requires classical reasoning.

以下等价关系被称为德摩根定律:

The following equivalences are known as De Morgan’s laws:

  • ¬(AB)¬A¬B

  • ¬(AB)¬A¬B

  • ¬(AB)¬A¬B

  • ¬(AB)¬A¬B

其中第二个问题的前进方向需要经典推理。

The forward direction of the second of these requires classical reasoning.

使用这些恒等式,我们总是可以将否定推到命题变量上。例如,我们有

Using these identities, we can always push negations down to propositional variables. For example, we have

公式建立于, 和¬其中否定仅发生在变量上被称为否定范式

A formula built up from , , and ¬ in which negations only occur at variables is said to be in negation normal form.

实际上,利用分配律,可以进一步确保所有析取都在外部,这样公式就是命题变量和否定命题变量的和的大或。这样的公式被称为析取范式。或者,所有和都可以放在外部。这样的公式被称为合取范式。然而,下面的练习表明,将公式置于析取或合取范式中会使它们变得更长。

In fact, using distributivity laws, one can go on to ensure that all the disjunctions are on the outside, so that the formulas is a big or of and’s of propositional variables and negated propositional variables. Such a formula is said to be in disjunctive normal form. Alternatively, all the and’s can be brought to the outside. Such a formula is said to be in conjunctive normal form. An exercise below, however, shows that putting formulas in disjunctive or conjunctive normal form can make them much longer.

5.3.练习

5.3. Exercises

  1. 说明如何利用其他自然演绎规则,从排中律推导出矛盾证明规则。换句话说,假设你有一个证明¬A。 使用A¬A作为假设,但使用规则 RAA,说明如何继续推导A

  2. Show how to derive the proof-by-contradiction rule from the law of the excluded middle, using the other rules of natural deduction. In other words, assume you have a proof of from ¬A. Using A¬A as a hypothesis, but without using the rule RAA, show how you can go on to derive A.

  3. 给出自然演绎证明¬(AB)¬A¬B.(你不需要使用反证法。)

  4. Give a natural deduction proof of ¬(AB) from ¬A¬B. (You do not need to use proof by contradiction.)

  5. 构造自然演绎证明¬A¬B¬(AB)。您可以按照如下方式进行操作:

    1. 首先,证明¬B,因此¬A¬B, 从¬(AB)A

    2. 使用它来构造一个证明¬A,因此¬A¬B, 从¬(AB)¬(¬A¬B)

    3. 用它来构造矛盾的证明¬(AB)¬(¬A¬B)

    4. 使用矛盾证明,这将为你提供证明¬A¬B¬(AB)

  6. Construct a natural deduction proof of ¬A¬B from ¬(AB). You can do it as follows:

    1. First, prove ¬B, and hence ¬A¬B, from ¬(AB) and A.

    2. Use this to construct a proof of ¬A, and hence ¬A¬B, from ¬(AB) and ¬(¬A¬B).

    3. Use this to construct a proof of a contradiction from ¬(AB) and ¬(¬A¬B).

    4. Using proof by contradiction, this gives you a proof of ¬A¬B from ¬(AB).

  7. 给出自然演绎证明P¬P(QR)¬Q, 和¬R

  8. Give a natural deduction proof of P from ¬P(QR), ¬Q, and ¬R.

  9. 给出自然演绎证明¬ABAB。你可以使用排中律。

  10. Give a natural deduction proof of ¬AB from AB. You may use the law of the excluded middle.

  11. 给出自然演绎证明A((AB)(A¬B))。你可以使用排中律。

  12. Give a natural deduction proof of A((AB)(A¬B)). You may use the law of the excluded middle.

  13. (AB)(CD)(EF)以析取范式形式,即写成多个“与”表达式的大“或”。

  14. Put (AB)(CD)(EF) in disjunctive normal form, that is, write it as a big “or” of multiple “and” expressions.

  15. 通过用证明代替下面的抱歉来证明。¬ (A B) ¬ A ¬ B

    open classical
    variables {A B C : Prop}
    
    -- Prove ¬ (A ∧ B) → ¬ A ∨ ¬ B by replacing the sorry's below
    -- by proofs.
    
    lemma step1 (h₁ : ¬ (A  B)) (h₂ : A) : ¬ A  ¬ B :=
    have ¬ B, from sorry,
    show ¬ A  ¬ B, from or.inr this
    
    lemma step2 (h₁ : ¬ (A  B)) (h₂ : ¬ (¬ A  ¬ B)) : false :=
    have ¬ A, from
      assume : A,
      have ¬ A  ¬ B, from step1 h₁ A›,
      show false, from h₂ this,
    show false, from sorry
    
    theorem step3 (h : ¬ (A  B)) : ¬ A  ¬ B :=
    by_contradiction
      (assume h' : ¬ (¬ A  ¬ B),
        show false, from step2 h h')
    
  16. Prove ¬ (A B) ¬ A ¬ B by replacing the sorry’s below by proofs.

    open classical
    variables {A B C : Prop}
    
    -- Prove ¬ (A ∧ B) → ¬ A ∨ ¬ B by replacing the sorry's below
    -- by proofs.
    
    lemma step1 (h₁ : ¬ (A  B)) (h₂ : A) : ¬ A  ¬ B :=
    have ¬ B, from sorry,
    show ¬ A  ¬ B, from or.inr this
    
    lemma step2 (h₁ : ¬ (A  B)) (h₂ : ¬ (¬ A  ¬ B)) : false :=
    have ¬ A, from
      assume : A,
      have ¬ A  ¬ B, from step1 h₁ A›,
      show false, from h₂ this,
    show false, from sorry
    
    theorem step3 (h : ¬ (A  B)) : ¬ A  ¬ B :=
    by_contradiction
      (assume h' : ¬ (¬ A  ¬ B),
        show false, from step2 h h')
    
  17. 还可以做这些:

    open classical
    variables {A B C : Prop}
    
    example (h : ¬ B  ¬ A) : A  B :=
    sorry
    
    example (h : A  B) : ¬ A  B :=
    sorry
    
  18. Also do these:

    open classical
    variables {A B C : Prop}
    
    example (h : ¬ B  ¬ A) : A  B :=
    sorry
    
    example (h : A  B) : ¬ A  B :=
    sorry
    

6.命题逻辑的语义

6. Semantics of Propositional Logic

传统上,我们认为命题变量涵盖了可能为真或为假的陈述。直观地,我们认为证明系统告诉我们什么命题公式必须为真,无论变量代表什么。例如,我们可以证明C从假设AB, 和ABC似乎告诉我们,只要假设成立,那么C也必须是真的。

Classically, we think of propositional variables as ranging over statements that can be true or false. And, intuitively, we think of a proof system as telling us what propositional formulas have to be true, no matter what the variables stand for. For example, the fact that we can prove C from the hypotheses A, B, and ABC seems to tell us that whenever the hypotheses are true, then C has to be true as well.

要理解这一点,就需要跳出系统,对真理做出解释——更准确地说,对命题公式为真的条件做出解释。这是符号逻辑被设计用来做的事情之一,而这项任务属于语义学的范畴。公式和形式证明是句法概念,也就是说,它们由符号和符号结构来表示。真理是一个语义概念,因为它将某种意义赋予某些公式。

Making sense of this involves stepping outside the system and giving an account of truth—more precisely, the conditions under which a propositional formula is true. This is one of the things that symbolic logic was designed to do, and the task belongs to the realm of semantics. Formulas and formal proofs are syntactic notions, which is to say, they are represented by symbols and symbolic structures. Truth is a semantic notion, in that it ascribes a type of meaning to certain formulas.

从句法上来说,我们能够提出和回答如下问题:

Syntactically, we were able to ask and answer questions like the following:

  • 给出一组假设,Γ,以及公式,A,我们可以得出AΓ

  • Given a set of hypotheses, Γ, and a formula, A, can we derive A from Γ?

  • 可以从哪些公式推导出Γ

  • What formulas can be derived from Γ?

  • 需要哪些假设来推导A

  • What hypotheses are needed to derive A?

我们在语义上考虑的问题是不同的:

The questions we consider semantically are different:

  • 为公式中出现的命题变量赋予真值A, 是A真还是假?

  • Given an assignment of truth values to the propositional variables occurring in a formula A, is A true or false?

  • 有没有真实的作业A真的?

  • Is there any truth assignment that makes A true?

  • 哪些是真实的作业,使得A真的?

  • Which are the truth assignments that make A true?

在本章中,我们不会提供完全严格的语法和语义数学处理。该主题适合更高级、更集中的数理逻辑课程。但我们将详细讨论语义问题,以便让您很好地理解语义思考的含义,以及如何务实地使用语义概念。

In this chapter, we will not provide a fully rigorous mathematical treatment of syntax and semantics. That subject matter is appropriate to a more advanced and focused course on mathematical logic. But we will discuss semantic issues in enough detail to give you a good sense of what it means to think semantically, as well as a sense of how to make pragmatic use of semantic notions.

6.1.真值和赋值

6.1. Truth Values and Assignments

我们首先需要的概念是真值。我们已经见过两个,即“真”和“假”。我们将使用符号TF用非正式数学来表示这些。这些是旨在用自然演绎来表示,而truefalse旨在用精益来表示。

The first notion we will need is that of a truth value. We have already seen two, namely, “true” and “false.” We will use the symbols T and F to represent these in informal mathematics. These are the values that and are intended to denote in natural deduction, and true and false are intended to denote in Lean.

在本文中,我们将采用“经典”的真理概念,就像我们在第 5 节中讨论的那样。这可以用各种方式来理解,但具体来说,可以归结为:我们假设任何命题要么是真,要么是假(当然,不能同时是真和假)。这种真理概念是排中律的基础,A¬A从语义上讲,我们把这句话理解为“要么A为真,或¬A是真的。”因为在我们的语义解释中,¬A恰好在A是错误的,排中律说A要么为真,要么为假。

In this text, we will adopt a “classical” notion of truth, following our discussion in Section 5. This can be understood in various ways, but, concretely, it comes down to this: we will assume that any proposition is either true or false (but, of course, not both). This conception of truth is what underlies the law of the excluded middle, A¬A. Semantically, we read this sentence as saying “either A is true, or ¬A is true.” Since, in our semantic interpretation, ¬A is true exactly when A is false, the law of the excluded middle says that A is either true or false.

我们需要的下一个概念是真值赋值,它只是一个为命题变量集合的每个元素分配真值的函数。在本节中,我们将使用字母来区分命题变量和任意公式P,Q,R,对于前者,A,B,C,对于后者。例如,函数v定义为

The next notion we will need is that of a truth assignment, which is simply a function that assigns a truth value to each element of a set of propositional variables. In this section, we will distinguish between propositional variables and arbitrary formulas by using letters P,Q,R, for the former and A,B,C, for the latter. For example, the function v defined by

  • v(P):=T

  • v(P):=T

  • v(Q):=F

  • v(Q):=F

  • v(R):=F

  • v(R):=F

  • v(S):=T

  • v(S):=T

是变量集的真值赋值{P,Q,R,S}

is a truth assignment for the set of variables {P,Q,R,S}.

直观地讲,真值分配描述了一种可能的“世界状态”。回到 Malice 和 Alice 之谜,我们假设以下字母是这些陈述的简写:

Intuitively, a truth assignment describes a possible “state of the world.” Going back to the Malice and Alice puzzle, let’s suppose the following letters are shorthand for the statements:

  • P:= 受害者是爱丽丝的兄弟

  • P := Alice’s brother was the victim

  • Q:= 爱丽丝是凶手

  • Q := Alice was the killer

  • R:= 爱丽丝在酒吧里

  • R := Alice was in the bar

在谜题解描述的世界中,第一和第三条陈述为真,第二条陈述为假。因此我们的真值赋值给出了TPR,以及价值FQ

In the world described by the solution to the puzzle, the first and third statements are true, and the second is false. So our truth assignment gives the value T to P and R, and the value F to Q.

一旦我们有了真相任务v一组命题变量,我们可以将其扩展为估值函数 v¯,它为每个仅依赖于这些变量的命题公式分配一个真或假的值。函数v¯是递归定义的,也就是说,公式是从下往上求值的,因此分配给复合公式的值由分配给其成分的值决定。该函数的形式定义如下:

Once we have a truth assignment v to a set of propositional variables, we can extend it to a valuation function v¯, which assigns a value of true or false to every propositional formula that depends only on these variables. The function v¯ is defined recursively, which is to say, formulas are evaluated from the bottom up, so that value assigned to a compound formula is determined by the values assigned to its components. Formally, the function is defined as follows:

  • v¯()=T

  • v¯()=T.

  • v¯()=F

  • v¯()=F.

  • v¯()=v(), 在哪里是任何命题变量。

  • v¯()=v(), where is any propositional variable.

  • v¯(¬A)=T如果v¯(A)F,反之亦然。

  • v¯(¬A)=T if v¯(A) is F, and vice versa.

  • v¯(AB)=T如果v¯(A)v¯(B)都是T, 和F否则。

  • v¯(AB)=T if v¯(A) and v¯(B) are both T, and F otherwise.

  • v¯(AB)=T如果至少有一个v¯(A)v¯(B)T; 否则F

  • v¯(AB)=T if at least one of v¯(A) and v¯(B) is T; otherwise F.

  • v¯(AB)=T如果v¯(B)T或者v¯(A)F, 和F否则。(等价地,v¯(AB)=F如果v¯(A)Tv¯(B)F, 和T否则。)

  • v¯(AB)=T if either v¯(B) is T or v¯(A) is F, and F otherwise. (Equivalently, v¯(AB)=F if v¯(A) is T and v¯(B) is F, and T otherwise.)

合取和析取的规则很容易理解。“AB” 恰好在以下情况下成立AB都是真的;”A或者B” 为真,当至少有一个A或者B是真的。

The rules for conjunction and disjunction are easy to understand. “A and B” is true exactly when A and B are both true; “A or B” is true when at least one of A or B is true.

理解蕴涵规则比较棘手。人们常常惊讶地听到,任何带有错误假设的“如果-那么”语句都应该是正确的。“如果我有两个头,那么圆就是正方形”这句话听起来应该是假的,但根据我们的推算,它是正确的。为了理解这一点,想想这两个句子之间的区别:

Understanding the rule for implication is trickier. People are often surprised to hear that any if-then statement with a false hypothesis is supposed to be true. The statement “if I have two heads, then circles are squares” may sound like it ought to be false, but by our reckoning, it comes out true. To make sense of this, think about the difference between the two sentences:

  • “如果我有两个头,那么圆形就是方形。”

  • “If I have two heads, then circles are squares.”

  • “如果我有两个头,那么圆形就会变成方形。”

  • “If I had two heads, then circles would be squares.”

第二句是反事实蕴涵的一个例子。它断言如果事情与实际情况不同,世界可能会发生怎样的变化。几个世纪以来,哲学家们一直在研究反事实,但数理逻辑关注的是第一句话,即实质性蕴涵。实质性蕴涵断言的是世界现在的样子,而不是它可能的样子。由于“我有两个头”是错误的,因此“如果我有两个头,那么圆就是正方形”这个说法是正确的。

The second sentence is an example of a counterfactual implication. It asserts something about how the world might change, if things were other than they actually are. Philosophers have studied counterfactuals for centuries, but mathematical logic is concerned with the first sentence, a material implication. The material implication asserts something about the way the world is right now, rather than the way it might have been. Since it is false that I have two heads, the statement “if I have two heads, then circles are squares” is true.

为什么我们要以这种方式评估实质蕴涵?让我们再次考虑这个真实句子“每个大于二的素数自然数都是奇数”。我们可以将这句话解释为,此列表中的所有(无穷多个)句子都是真实的:

Why do we evaluate material implication in this way? Once again, let us consider the true sentence “every natural number that is prime and greater than two is odd.” We can interpret this sentence as saying that all of the (infinitely many) sentences in this list are true:

  • 如果 0 为质数且大于 2,则 0 为奇数。

  • If 0 is prime and greater than 2, then 0 is odd.

  • 如果 1 为质数且大于 2,则 1 为奇数。

  • If 1 is prime and greater than 2, then 1 is odd.

  • 如果 2 为质数且大于 2,则 2 为奇数。

  • If 2 is prime and greater than 2, then 2 is odd.

  • 如果 3 为质数且大于 2,则 3 为奇数。

  • If 3 is prime and greater than 2, then 3 is odd.

此列表中的第一句话与我们的“两个头”示例非常相似,因为假设和结论都是错误的。但由于它是一个一般情况下为真的陈述的实例,因此我们致力于赋予它值T第二句则不同:假设仍然为假,但结论为真。总之,这些句子告诉我们,只要假设为假,条件语句就应该为真。第四句的假设和结论都为真。所以从第二句和第四句中,我们看到,只要结论为真,条件语句也应该为真。最后,似乎很清楚,“如果 3 为素数且大于 2,则 3 为偶数”这句话不应该真。这种假设为真而结论为假的模式是唯一一种条件语句为假的模式。

The first sentence on this list is a lot like our “two heads” example, since both the hypothesis and the conclusion are false. But since it is an instance of a statement that is true in general, we are committed to assigning it the value T. The second sentence is a different: the hypothesis is still false, but here the conclusion is true. Together, these tell us that whenever the hypothesis is false, the conditional statement should be true. The fourth sentence has a true hypothesis and a true conclusion. So from the second and fourth sentences, we see that whenever the conclusion is true, the conditional should be true as well. Finally, it seems clear that the sentence “if 3 is prime and greater than 2, then 3 is even” should not be true. This pattern, where the hypothesis is true and the conclusion is false, is the only one for which the conditional will be false.

让我们用另一种方式来激发实质蕴涵的语义,使用上一章中描述的演绎规则。请注意,如果B是真的,我们可以证明AB没有任何假设A

Let us motivate the semantics for material implication another way, using the deductive rules described in the last chapter. Notice that, if B is true, we can prove AB without any assumptions about A:

这是从正确解读蕴涵引入规则得出的:给定B,人们总能推断AB,然后取消假设A如果有的话。如果A在证明中从未使用过,结论只是比它需要的更弱。这个推论在 Lean 中得到验证:

This follows from the proper reading of the implication introduction rule: given B, one can always infer AB, and then cancel an assumption A, if there is one. If A was never used in the proof, the conclusion is simply weaker than it needs to be. This inference is validated in Lean:

variables A B : Prop
variable hB : B

example : A  B :=
assume hA : A,
  show B, from hB
variables A B : Prop
variable hB : B

example : A  B :=
assume hA : A,
  show B, from hB

类似地,如果A是假的,我们可以证明AB没有任何假设B

Similarly, if A is false, we can prove AB without any assumptions about B:

在精益领域:

In Lean:

variables A B : Prop
variable hnA : ¬ A

example : A  B :=
assume hA : A,
  show B, from false.elim (hnA hA)
variables A B : Prop
variable hnA : ¬ A

example : A  B :=
assume hA : A,
  show B, from false.elim (hnA hA)

最后,如果A是真的,并且B是假的,我们可以证明¬(AB)

Finally, if A is true and B is false, we can prove ¬(AB):

再次强调,在精益领域:

Once again, in Lean:

variables A B : Prop
variable hA : A
variable hnB : ¬B

example : ¬ (A  B) :=
assume h : A  B,
have hB : B, from h hA,
show false, from hnB hB
variables A B : Prop
variable hA : A
variable hnB : ¬B

example : ¬ (A  B) :=
assume h : A  B,
have hB : B, from h hA,
show false, from hnB hB

现在我们已经定义了任何公式相对于真值赋值的真值,我们可以回答第一个语义问题了:给定一个赋值v真值对出现在某个公式中的命题变量φ,我们如何确定φ是真的吗?这相当于评估v¯(φ)以及递归定义φ给出了一个配方:我们评估出现在φ从下往上,从命题变量开始,使用表达式组件的求值来求表达式本身的值。例如,假设我们的真值赋值v使AB真实且C错误。评估(BC)(AB)在下面v,请注意表达式BC结果是错误的,并且表达式AB结果为真。由于“假或真”析取为真,因此整个公式为真。

Now that we have defined the truth of any formula relative to a truth assignment, we can answer our first semantic question: given an assignment v of truth values to the propositional variables occurring in some formula φ, how do we determine whether or not φ is true? This amounts to evaluating v¯(φ), and the recursive definition of φ gives a recipe: we evaluate the expressions occurring in φ from the bottom up, starting with the propositional variables, and using the evaluation of an expression’s components to evaluate the expression itself. For example, suppose our truth assignment v makes A and B true and C false. To evaluate (BC)(AB) under v, note that the expression BC comes out false and the expression AB comes out true. Since a disjunction “false or true” is true, the entire formula is true.

我们也可以反过来:给定一个公式,我们可以尝试找到一个使其为真(或为假)的真值赋值。事实上,我们可以使用 Lean 来为我们评估公式。在下面的例子中,你可以为命题符号ABCD和分配任意一组值E。当你在这个输入上运行 Lean 时,语句的输出eval就是表达式的值。

We can also go in the other direction: given a formula, we can attempt to find a truth assignment that will make it true (or false). In fact, we can use Lean to evaluate formulas for us. In the example that follows, you can assign any set of values to the proposition symbols A, B, C, D, and E. When you run Lean on this input, the output of the eval statement is the value of the expression.

-- Define your truth assignment here
def A := tt
def B := ff
def C := tt
def D := tt
def E := ff

def test (p : Prop) [decidable p] : string :=
if p then "true" else "false"

#eval test ((A  B)  ¬ C)
#eval test (A  D)
#eval test (C  (D  ¬E))
#eval test (¬(A  B  C  D))
-- Define your truth assignment here
def A := tt
def B := ff
def C := tt
def D := tt
def E := ff

def test (p : Prop) [decidable p] : string :=
if p then "true" else "false"

#eval test ((A  B)  ¬ C)
#eval test (A  D)
#eval test (C  (D  ¬E))
#eval test (¬(A  B  C  D))

尝试改变真值赋值,看看会发生什么。您可以在输入的末尾添加自己的公式,并对其进行求值。尝试找到使上面测试的每个公式求值为真的真值赋值。为了增加挑战性,尝试找到一个使它们同时为真的真值赋值。

Try varying the truth assignments, to see what happens. You can add your own formulas to the end of the input, and evaluate them as well. Try to find truth assignments that make each of the formulas tested above evaluate to true. For an extra challenge, try finding a single truth assignment that makes them all true at the same time.

6.2.真值表

6.2. Truth Tables

我们提出的第二和第三个语义问题比第一个问题稍微棘手一些。给定一个公式A,是否存在任何真实分配使得A正确吗?如果正确,哪些真理分配使得A真的吗?这些问题不考虑某一个特定的真理分配,而是要求我们量化所有可能的真理分配。

The second and third semantic questions we asked are a little trickier than the first. Given a formula A, is there any truth assignment that makes A true? If so, which truth assignments make A true? Instead of considering one particular truth assignment, these questions ask us to quantify over all possible truth assignments.

当然,可能的真值分配的数量取决于我们正在考虑的命题字母的数量。由于每个字母有两个可能的值,n字母将产生2n可能的真实分配。这个数字增长得非常快,因此我们在这里主要关注较小的公式。

Of course, the number of possible truth assignments depends on the number of propositional letters we’re considering. Since each letter has two possible values, n letters will produce 2n possible truth assignments. This number grows very quickly, so we’ll mostly look at smaller formulas here.

我们将使用真值表判断公式是否为真。在真值表的左侧,我们将列出当前命题字母的所有可能的真值赋值。在右侧,我们将整个公式的真值放在相应的赋值下。

We’ll use something called a truth table to figure out when, if ever, a formula is true. On the left hand side of the truth table, we’ll put all of the possible truth assignments for the present propositional letters. On the right hand side, we’ll put the truth value of the entire formula under the corresponding assignment.

首先,真值表可以用来简洁地总结我们的逻辑连接词的语义:

To begin with, truth tables can be used to concisely summarize the semantics of our logical connectives:

我们将留给你来写表格¬A,作为一项简单的练习。

We will leave it to you to write the table for ¬A, as an easy exercise.

对于复合公式,样式大致相同。有时,在中间列中包含子公式的真值可能会有所帮助:

For compound formulas, the style is much the same. Sometimes it can be helpful to include intermediate columns with the truth values of subformulas:

通过写出公式的真值表,我们可以浏览行并查看哪些真值赋值使公式为真。如果最后一列中的所有条目都是T,如上例所示,该公式即为有效

By writing out the truth table for a formula, we can glance at the rows and see which truth assignments make the formula true. If all the entries in the final column are T, as in the above example, the formula is said to be valid.

6.3.健全性和完整性

6.3. Soundness and Completeness

假设我们心中有一个固定的演绎系统,比如自然演绎。如果该系统中有命题公式的形式证明,则称该命题公式是可证明的。如果命题公式在任何真值赋值下都为真,则称其为同义反复有效。可证明性是一个句法概念,因为它断言存在一个句法对象,即证明。有效性是一个语义概念,因为它与真值赋值和估值有关。但直观上看,这些概念应该是一致的:两者都表达了公式A 必须是真的,或者必然是真的,并且人们期望一个好的证明系统能够让我们推导出有效的公式。

Suppose we have a fixed deduction system in mind, such as natural deduction. A propositional formula is said to be provable if there is a formal proof of it in that system. A propositional formula is said to be a tautology, or valid, if it is true under any truth assignment. Provability is a syntactic notion, in that it asserts the existence of a syntactic object, namely, a proof. Validity is a semantic notion, in that it has to do with truth assignments and valuations. But, intuitively, these notions should coincide: both express the idea that a formula A has to be true, or is necessarily true, and one would expect a good proof system to enable us to derive the valid formulas.

每一个可证明公式都是有效的,这个说法被称为可靠性。如果A是任何公式,逻辑学家使用符号A表达这一事实A是可证明的,并且符号A表达A是有效的。(第一个符号有时被称为“旋转门”,第二个符号有时被称为“双旋转门”。)用这种符号表示,健全性是指对于每个命题公式A, 如果A, 然后A. 逆命题,即每个有效公式都是可证明的,被称为完备性。用符号表示,对于每个公式A, 如果A, 然后A

The statement that every provable formula is valid is known as soundness. If A is any formula, logicians use the notation A to express the fact that A is provable and the notation A to express that A is valid. (The first symbol is sometimes called a “turnstile” and the second symbol is sometimes called a “double-turnstile.”) With this notation, soundness says that for every propositional formula A, if A, then A. The converse, which says that every valid formula is provable, is known as completeness. In symbolic terms, it says that for every formula A, if A, then A.

由于我们选择推理规则和定义估值概念的方式,这两个概念应该一致的直觉是正确的。换句话说,我们为命题逻辑提出的自然演绎系统在真值表语义方面是健全和完整的。

Because of the way we have chosen our inference rules and defined the notion of a valuation, this intuition that the two notions should coincide holds true. In other words, the system of natural deduction we have presented for propositional logic is sound and complete with respect to truth-table semantics.

这些健全性和完整性的概念延伸到假设的可证明性。如果Γ是一组命题公式,并且A是一个命题公式,那么A认为是Γ如果给定任何真值赋值,使得每个公式Γ真的,A也是正确的。在这个扩展的设置中,健全性是指如果A可以证明Γ, 然后A是合乎逻辑的结果Γ完整性则相反:如果A是合乎逻辑的结果Γ,可以证明Γ用符号表示,我们写成ΓA表达A可以从公式中证明Γ(或者Γ 证明 A),我们写ΓA表达A是合乎逻辑的结果Γ(或者Γ 包含 A)。 用这种符号表示,健全性是指对于每个命题公式A和一组命题公式Γ, 如果ΓA然后ΓA,而完整性则表示对于每一个AΓ, 如果ΓA然后ΓA

These notions of soundness and completeness extend to provability from hypotheses. If Γ is a set of propositional formulas and A is a propositional formula, then A is said to be a logical consequence of Γ if, given any truth assignment that makes every formula in Γ true, A is true as well. In this extended setting, soundness says that if A is provable from Γ, then A is a logical consequence of Γ. Completeness runs the other way: if A is a logical consequence of Γ, it is provable from Γ. In symbolic terms, we write ΓA to express that A is provable from the formulas in Γ (or that Γ proves A), and we write ΓA to express that A is a logical consequence of Γ (or that Γ entails A). With this notation, soundness says that for every propositional formula A and set of propositional formulas Γ, if ΓA then ΓA, and completeness says that for every A and Γ, if ΓA then ΓA.

给定一组命题公式Γ和一个命题公式A上一节为我们提供了一个方法来决定Γ包含A:为所有公式构建真值表ΓA,并检查是否每一个A在表格的每一行上,每个公式都是正确的Γ是真的。(不管发生什么A在某些公式中Γ是错误的。

Given a set of propositional formulas Γ and a propositional formula A, the previous section gives us a recipe for deciding whether Γ entails A: construct a truth tables for all the formulas in Γ and A, and check whether every A comes out true on every line of the table on which every formula of Γ is true. (It doesn’t matter what happens to A on the lines where some formula in Γ is false.)

请注意,根据自然演绎规则,公式A可以通过一系列假设来证明{B1,B2,,Bn}当且仅当公式B1B2BnA是可以直接证明的,也就是说,不需要任何假设。因此,至少对于有限公式集来说Γ,健全性和完备性两个表述是等价的。

Notice that with the rules of natural deduction, a formula A is provable from a set of hypotheses {B1,B2,,Bn} if and only if the formula B1B2BnA is provable outright, that is, from no hypotheses. So, at least for finite sets of formulas Γ, the two statements of soundness and completeness are equivalent.

证明可靠性和完整性属于元理论的范畴,因为它要求我们推理我们的推理方法。这不是本书的重点:我们更关心的是使用逻辑和真理概念,而不是确定它们的属性。但可靠性和完整性的概念在帮助我们理解逻辑概念的性质方面发挥着重要作用,因此我们将尝试在这里提供一些提示,说明为什么这些属性适用于命题逻辑。

Proving soundness and completeness belongs to the realm of metatheory, since it requires us to reason about our methods of reasoning. This is not a central focus of this book: we are more concerned with using logic and the notion of truth than with establishing their properties. But the notions of soundness and completeness play an important role in helping us understand the nature of the logical notions, and so we will try to provide some hints here as to why these properties hold for propositional logic.

证明可靠性比证明完整性更容易。我们希望证明,无论何时A可以通过一系列假设来证明,Γ, 然后A是合乎逻辑的结果Γ在后面的章节中,我们将考虑归纳证明,归纳证明允许我们通过证明某个属性对一些“简单”对象成立,并在更复杂的对象转变过程中保持不变,来建立一般对象集合的属性。在自然演绎的情况下,只需证明合理性对最基本的证明成立(使用假设规则),并且在每个推理规则下都保持不变。基本情况很简单:假设规则说A可以通过假设证明A,显然每一个真相任务A真正的A正确。归纳步骤并不难;它们涉及检查我们选择的规则是否与语义概念相吻合。例如,假设最后一条规则是 and-introduction 规则。在这种情况下,我们有A从一些假设来看Γ,以及B从一些假设来看Δ,我们把它们结合起来形成一个证明AB从假设ΓΔ,即两者中的假设。归纳地,我们可以假设A是合乎逻辑的结果Γ而且B是合乎逻辑的结果Δ。 让v是任何真值赋值,使得ΓΔ正确。然后根据归纳假设,我们有A正确,并且B正确。根据估值函数的定义,v¯(AB)=T,按要求。

Proving soundness is easier than proving completeness. We wish to show that whenever A is provable from a set of hypotheses, Γ, then A is a logical consequence of Γ. In a later chapter, we will consider proofs by induction, which allows us to establish a property holds of a general collection of objects by showing that it holds of some “simple” ones and is preserved under the passage to objects that are more complex. In the case of natural deduction, it is enough to show that soundness holds of the most basic proofs—using the assumption rule—and that it is preserved under each rule of inference. The base case is easy: the assumption rule says that A is provable from hypothesis A, and clearly every truth assignment that makes A true makes A true. The inductive steps are not much harder; they involve checking that the rules we have chosen mesh with the semantic notions. For example, suppose the last rule is the and-introduction rule. In that case, we have a proof of A from some hypotheses Γ, and a proof of B from some hypotheses Δ, and we combine these to form a proof of AB from the hypotheses in ΓΔ, that is, the hypotheses in both. Inductively, we can assume that A is a logical consequence of Γ and that B is a logical consequence of Δ. Let v be any truth assignment that makes every formula in ΓΔ true. Then by the inductive hypothesis, we have that it makes A true, and B true as well. By the definition of the valuation function, v¯(AB)=T, as required.

证明完整性更难。只要证明A是任何重言式,那么A是可证明的。一种策略是表明自然演绎可以模拟真值表的方法。例如,假设A由命题变量构成BC。那么在自然演绎中,我们应该能够证明

Proving completeness is harder. It suffices to show that if A is any tautology, then A is provable. One strategy is to show that natural deduction can simulate the method of truth tables. For example, suppose A is build up from propositional variables B and C. Then in natural deduction, we should be able to prove

(BC)(B¬C)(¬BC)(¬B¬C),

真值表的每一行都有一个析取项。然后,我们应该能够使用每个析取项来“评估”出现在A,根据其估值证明其真假,直到我们有证据A本身。

with one disjunct for each line of the truth table. Then, we should be able to use each disjunct to “evaluate” each expression occurring in A, proving it true or false in accordance with its valuation, until we have a proof of A itself.

一个更好的方法是,以一种允许我们从A寻找证明。换句话说,首先,我们给出一个构造A通过逆向工作A。然后我们论证说,如果程序失败,那么在失败的地方,我们可以找到一个真值分配,使得A假。因此,如果每个真值赋值都Atrue,该过程返回一个证明A

A nicer way to proceed is to express the rules of natural deduction in a way that allows us to work backward from A in search of a proof. In other words, first, we give a procedure for constructing a derivation of A by working backward from A. Then we argue that if the procedure fails, then, at the point where it fails, we can find a truth assignment that makes A false. As a result, if every truth assignment makes A true, the procedure returns a proof of A.

6.4.练习

6.4. Exercises

  1. 表明AB¬AB, 和¬(A¬B)通过写出真值表并表明它们对于所有真值分配具有相同的值,它们在逻辑上是等价的。

  2. Show that AB, ¬AB, and ¬(A¬B) are logically equivalent, by writing out the truth table and showing that they have the same values for all truth assignments.

  3. 写出真值表(AB)(BCA)

  4. Write out the truth table for (AB)(BCA).

  5. 表明AB¬B¬A是等效的,通过写出真值表并表明它们对于所有真值分配具有相同的值。

  6. Show that AB and ¬B¬A are equivalent, by writing out the truth tables and showing that they have the same values for all truth assignments.

  7. 以下蕴涵成立吗?

    {ABC,¬B¬C}AB

    通过写出真值表(抱歉,太长了)来证明你的答案。清楚地标出两个假设都成立的行。

  8. Does the following entailment hold?

    {ABC,¬B¬C}AB

    Justify your answer by writing out the truth table (sorry, it is long). Indicate clearly the rows where both hypotheses come out true.

  9. 以下公式可以推导吗?请用推导或反例证明你的答案。

    • ¬(¬AB)A

    • (¬A¬B)(AB)

    • ((PQ)R)(R¬P)

    • (¬P¬Q)¬(QP)

  10. Are the following formulas derivable? Justify your answer with either a derivation or a counterexample.

    • ¬(¬AB)A

    • (¬A¬B)(AB)

    • ((PQ)R)(R¬P)

    • (¬P¬Q)¬(QP)

7.一阶逻辑

7. First Order Logic

命题逻辑为描述逻辑推理的一般原则提供了一个良好的开端,但还远远不够。即使在第 2 章的“恶意和爱丽丝”例子中,它的某些限制也显而易见。命题逻辑不能为我们提供表达以下一般原则的方法:如果爱丽丝和她的儿子在海滩上,那么她的儿子也和爱丽丝在一起;没有孩子比父母年长这个一般事实;或者如果某人独自一人,那么他们就不会和别人在一起这个一般事实。为了表达这样的原则,我们需要一种方式来谈论对象和个体,以及它们的属性和它们之间的关系。这些正是更具表现力的逻辑框架所提供的,该框架称为一阶逻辑,将是接下来几章的主题。

Propositional logic provides a good start at describing the general principles of logical reasoning, but it does not go far enough. Some of the limitations are apparent even in the “Malice and Alice” example from Chapter 2. Propositional logic does not give us the means to express a general principle that tells us that if Alice is with her son on the beach, then her son is with Alice; the general fact that no child is older than his or her parent; or the general fact that if someone is alone, they are not with someone else. To express principles like these, we need a way to talk about objects and individuals, as well as their properties and the relationships between them. These are exactly what is provided by a more expressive logical framework known as first-order logic, which will be the topic of the next few chapters.

7.1.函数、谓词和关系

7.1. Functions, Predicates, and Relations

考虑一些关于自然数的普通陈述:

Consider some ordinary statements about the natural numbers:

  • 每个自然数要么是偶数,要么是奇数,但不能同时是偶数和奇数。

  • Every natural number is even or odd, but not both.

  • 自然数当且仅当它能被二整除才是偶数。

  • A natural number is even if and only if it is divisible by two.

  • 如果某个自然数x,是偶数,那么也是x2

  • If some natural number, x, is even, then so is x2.

  • 自然数x当且仅当x+1很奇怪。

  • A natural number x is even if and only if x+1 is odd.

  • 任何大于 2 的质数都是奇数。

  • Any prime number that is greater than 2 is odd.

  • 对于任意三个自然数xy, 和z, 如果x分裂yy分裂z, 然后x分裂z

  • For any three natural numbers x, y, and z, if x divides y and y divides z, then x divides z.

这些陈述是正确的,但我们一般不认为它们在逻辑上有效:它们依赖于对自然数、术语“偶数”和“奇数”的含义等的假设。但是,例如,一旦我们接受了第一个陈述,白宫楼梯的数量要么是偶数要么是奇数,这似乎是一个合乎逻辑的结果,特别是,如果不是偶数,那就是奇数。为了理解这样的推论,我们需要一个能够处理对象、它们的属性以及它们之间的关系的逻辑系统。

These statements are true, but we generally do not think of them as logically valid: they depend on assumptions about the natural numbers, the meaning of the terms “even” and “odd,” and so on. But once we accept the first statement, for example, it seems to be a logical consequence that the number of stairs in the White House is either even or odd, and, in particular, if it is not even, it is odd. To make sense of inferences like these, we need a logical system that can deal with objects, their properties, and relations between them.

一阶逻辑不是一劳永逸地解决单一语言问题,而是允许我们为任何给定的感兴趣领域指定我们希望使用的符号。在本节中,我们将使用以下运行示例:

Rather than fix a single language once and for all, first-order logic allows us to specify the symbols we wish to use for any given domain of interest. In this section, we will use the following running example:

  • 感兴趣的领域是自然数,N

  • The domain of interest is the natural numbers, N.

  • 有物体,0123,…。

  • There are objects, 0, 1, 2, 3, ….

  • 这个域上有函数、加法和乘法,还有平方函数。

  • There are functions, addition and multiplication, as well as the square function, on this domain.

  • 这个领域有谓词“偶数”,“奇数”和“素数”。

  • There are predicates on this domain, “even,” “odd,” and “prime.”

  • 该领域的元素之间存在“等于”,“小于”和“除”的关系。

  • There are relations between elements of this domain, “equal,” “less than”, and “divides.”

对于我们的逻辑语言,我们将选择符号 1、2、3,addmulsquareevenoddprimelt等等,来表示这些东西。我们也会用变量xy, 和z遍历自然数。请注意以下所有内容。

For our logical language, we will choose symbols 1, 2, 3, add, mul, square, even, odd, prime, lt, and so on, to denote these things. We will also have variables x, y, and z ranging over the natural numbers. Note all of the following.

  • 函数可以接受不同数量的参数:如果xy是自然数,那么写成mul(x,y)square(x)。 所以mul接受两个参数,并且square仅需一个。

  • Functions can take different numbers of arguments: if x and y are natural numbers, it makes sense to write mul(x,y) and square(x). So mul takes two arguments, and square takes only one.

  • 谓词和关系也可以用这些术语来理解。谓词even(x)prime(x)接受一个参数,而二元关系divides(x,y)lt(x,y)采取两个参数。

  • Predicates and relations can also be understood in these terms. The predicates even(x) and prime(x) take one argument, while the binary relations divides(x,y) and lt(x,y) take two arguments.

  • 函数与谓词不同!函数接受一个或多个参数,并返回一个。谓词接受一个或多个参数,并且结果要么为真,要么为假。我们可以将谓词视为返回命题,而不是值。

  • Functions are different from predicates! A function takes one or more arguments, and returns a value. A predicate takes one or more arguments, and is either true or false. We can think of predicates as returning propositions, rather than values.

  • 事实上,我们可以认为常数符号1,2,3,作为特殊类型的函数符号,它们接受零个参数。类似地,我们可以将接受零个参数的谓词视为常量逻辑值,

  • In fact, we can think of the constant symbols 1,2,3, as special sorts of function symbols that take zero arguments. Analogously, we can consider the predicates that take zero arguments to be the constant logical values, and .

  • 在普通数学中,我们经常使用“中缀”符号来表示二元函数和关系。例如,我们通常写成x×y或者xy而不是mul(x,y),我们写x<y而不是lt(x,y)。在自然演绎中编写证明时,我们将使用这些约定,它们在 Lean 中也得到支持。

  • In ordinary mathematics, we often use “infix” notation for binary functions and relations. For example, we usually write x×y or xy instead of mul(x,y), and we write x<y instead of lt(x,y). We will use these conventions when writing proofs in natural deduction, and they are supported in Lean as well.

  • 我们将处理平等关系,x=y,作为包含在每个一阶语言中的特殊二元关系。

  • We will treat the equality relation, x=y, as a special binary relation that is included in every first-order language.

一阶逻辑允许我们用基本表达式构建复杂表达式。从变量和常量开始,我们可以使用函数符号构建如下复合表达式:

First-order logic allows us to build complex expressions out of the basic ones. Starting with the variables and constants, we can use the function symbols to build up compound expressions like these:

  • x+y+z

  • x+y+z

  • (x+1)×y×y

  • (x+1)×y×y

  • square(x+y×z)

  • square(x+y×z)

此类表达被称为“术语”。直观上,它们命名了预期论域中的对象。

Such expressions are called “terms.” Intuitively, they name objects in the intended domain of discourse.

现在,使用谓词和关系符号,我们可以对这些表达式做出断言:

Now, using the predicates and relation symbols, we can make assertions about these expressions:

  • even(x+y+z)

  • even(x+y+z)

  • prime((x+1)×y×y)

  • prime((x+1)×y×y)

  • square(x+y×z)=w

  • square(x+y×z)=w

  • x+y<z

  • x+y<z

更有趣的是,我们可以使用命题连接词来构建如下复合表达式:

Even more interestingly, we can use propositional connectives to build compound expressions like these:

  • even(x+y+z)prime((x+1)×y×y)

  • even(x+y+z)prime((x+1)×y×y)

  • ¬(square(x+y×z)=w)x+y<z

  • ¬(square(x+y×z)=w)x+y<z

  • x<yeven(x)even(y)x+1<y

  • x<yeven(x)even(y)x+1<y

例如,第二个断言(x+yz)2不等于w, 或者x+y小于z。请记住,这些是符号逻辑中的表达式;在普通数学中,我们会像上面一样使用“是偶数”和“当且仅当”等词语来表达这些概念。只要我们处于符号逻辑领域,我们就会使用这样的符号,例如,当我们在自然演绎中写出证明时。像这样的表达式称为公式。与命名事物的术语相反,公式说事物;换句话说,它们对论域中的对象做出断言。

The second one, for example, asserts that either (x+yz)2 is not equal to w, or x+y is less than z. Remember, these are expressions in symbolic logic; in ordinary mathematics, we would express the notions using words like “is even” and “if and only if,” as we did above. We will use notation like this whenever we are in the realm of symbolic logic, for example, when we write proofs in natural deduction. Expressions like these are called formulas. In contrast to terms, which name things, formulas say things; in other words, they make assertions about objects in the domain of discourse.

7.2.通用量词

7.2. The Universal Quantifier

一阶逻辑的强大之处在于它允许我们使用量词做出一般性断言。全称量词后面跟着一个变量x是为了代表“对于每一个x换句话说,它断言x具有其后的属性。使用全称量词,我们在上一节开头所举的例子可以表达如下:

What makes first-order logic powerful is that it allows us to make general assertions using quantifiers. The universal quantifier followed by a variable x is meant to represent the phrase “for every x.” In other words, it asserts that every value of x has the property that follows it. Using the universal quantifier, the examples with which we began the previous section can be expressed as follows:

  • x((even(x)odd(x))¬(even(x)odd(x)))

  • x((even(x)odd(x))¬(even(x)odd(x)))

  • x(even(x)2x)

  • x(even(x)2x)

  • x(even(x)even(x2))

  • x(even(x)even(x2))

  • x(even(x)odd(x+1))

  • x(even(x)odd(x+1))

  • x(prime(x)x>2odd(x))

  • x(prime(x)x>2odd(x))

  • xyz(xyyzxz)

  • xyz(xyyzxz)

通常会将多个相同类型的量词组合起来,例如:x,y,z(xyyzxz)在最后一个表达式中。

It is common to combine multiple quantifiers of the same kind, and write, for example, x,y,z(xyyzxz) in the last expression.

以下是一些有关语法的说明:

Here are some notes on syntax:

  • 在符号逻辑中,全称量词通常被认为具有紧密结合性。例如,xPQ被解释为(xP)Q,我们会写x(PQ)以扩大范围。

  • In symbolic logic, the universal quantifier is usually taken to bind tightly. For example, xPQ is interpreted as (xP)Q, and we would write x(PQ) to extend the scope.

  • 但要小心。在其他情况下,尤其是在计算机科学中,人们通常会赋予量词尽可能宽的范围。精益就是这种情况。例如,被解释为,我们会写成限制范围。 x, P Q x, (P Q)(∀ x, P) Q

  • Be careful, however. In other contexts, especially in computer science, people often give quantifiers the widest scope possible. This is the case with Lean. For example, x, P Q is interpreted as x, (P Q), and we would write (∀ x, P) Q to limit the scope.

  • 当你把量词x前面有一个涉及变量的公式x,该变量的所有出现都受量词约束。例如,表达式x(even(x)odd(x))表示每个数字都是偶数或奇数。请注意变量x在非正式声明中没有出现。该声明不是关于x一点也不;而是x是一个虚拟变量,一个占位符,代表短语中提到的“事物”,该短语以“每件事”一词开头。我们想到这个表达式x(even(x)odd(x))与表达式相同y(even(y)odd(y)).Lean 也把这些表达视为相同。

  • When you put the quantifier x in front a formula that involves the variable x, all the occurrences of that variable are bound by the quantifier. For example, the expression x(even(x)odd(x)) is expresses that every number is even or odd. Notice that the variable x does not appear anywhere in the informal statement. The statement is not about x at all; rather x is a dummy variable, a placeholder that stands for the “thing” referred to within a phrase that beings with the words “every thing.” We think of the expression x(even(x)odd(x)) as being the same as the expression y(even(y)odd(y)). Lean also treats these expressions as the same.

  • 在精益中,表达式被解释为,右边带有括号。因此,表达式中全称量词后面的部分可以解释为“假设 除以 ,并且除以,则除以。”该表达式在逻辑上等同于,但我们将会看到,在精益中,将这样的事实表达为迭代蕴涵通常很方便。 x y z, x y y z x z x y z, x y (y z x z)xyyzxz x y z, x y y z x z

  • In Lean, the expression x y z, x y y z x z is interpreted as x y z, x y (y z x z), with parentheses associated to the right. The part of the expression after the universal quantifier can therefore be interpreted as saying “given that x divides y and that y divides z, x divides z.” The expression is logically equivalent to x y z, x y y z x z, but we will see that, in Lean, it is often convenient to express facts like this as an iterated implication.

不受约束的变量称为自由变量。请注意,一阶逻辑中的公式会说明其自由变量的情况。例如,在我们想到的解释中,公式y(xy)x小于或等于每个自然数。公式z(xz)完全一样;我们总是可以重命名绑定变量,只要我们选择一个不与已使用的另一个名称冲突的名称。另一方面,公式y(wy)w小于或等于每个自然数。这是一个完全不同的陈述:它说明了w,而不是x。因此,重命名自由变量会改变公式的含义。

A variable that is not bound is called free. Notice that formulas in first-order logic say things about their free variables. For example, in the interpretation we have in mind, the formula y(xy) says that x is less than or equal to every natural number. The formula z(xz) says exactly the same thing; we can always rename a bound variable, as long as we pick a name that does not clash with another name that is already in use. On the other hand, the formula y(wy) says that w is less than or equal to every natural number. This is an entirely different statement: it says something about w, rather than x. So renaming a free variable changes the meaning of a formula.

还要注意一些公式,例如x,y(xyyx),根本没有自由变量。这样的公式被称为句子因为它做出了一个直接的断言,即对预期解释来说要么是真要么是假的陈述。在第 10 章中,我们将明确“预期解释”的概念,并解释“在解释中为真”的含义。目前,公式在预期解释中对对象进行描述的想法应该可以激发使用此类表达式进行推理的规则。

Notice also that some formulas, like x,y(xyyx), have no free variables at all. Such a formula is called a sentence, because it makes an outright assertion, a statement that is either true or false about the intended interpretation. In Chapter 10 we will make the notion of an “intended interpretation” precise, and explain what it means to be “true in an interpretation.” For now, the idea that formulas say things about an object in an intended interpretation should motivate the rules for reasoning with such expressions.

第 1 章中,我们证明了 2 的平方根是无理数。解释该陈述的一种方式如下:

In Chapter 1 we proved that the square root of two is irrational. One way to construe the statement is as follows:

对于每一对整数,ab, 如果b0,事实并非如此a2=2b2

For every pair of integers, a and b, if b0, it is not the case that a2=2b2.

这种表述的优点是,我们可以将注意力限制在整数上,而不必考虑更大的有理数域。在符号逻辑中,假设我们想要讨论的域是整数,我们将使用全称量词来表达这个定理:

The advantage of this formulation is that we can restrict our attention to the integers, without having to consider the larger domain of rationals. In symbolic logic, assuming our intended domain of discourse is the integers, we would express this theorem using the universal quantifier:

a,bb0¬(a2=2b2).

请注意,我们保留了传统的数学符号b0b不等于 0,但我们可以把它看作是¬(b=0)我们如何证明这样的定理?非正式地,我们将使用这样的模式:

Notice that we have kept the conventional mathematical notation b0 to say that b is not equal to 0, but we can think of this as an abbreviation for ¬(b=0). How do we prove such a theorem? Informally, we would use such a pattern:

ab为任意整数,假设b0,假设a2=2b2

矛盾。

Let a and b be arbitrary integers, suppose b0, and suppose a2=2b2.

Contradiction.

我们实际上在做的是证明普遍命题成立,通过表明它对“任意”值都成立ab在自然演绎中,证明看起来是这样的:

What we are really doing is proving that the universal statement holds, by showing that it holds of “arbitrary” values a and b. In natural deduction, the proof would look something like this:

注意,在取消假设之后,我们证明了b0¬(a2=2×b2)不做任何假设ab;在证明的这个阶段,它们是“任意的”,证明了在接下来的两个规则中应用全称量词的合理性。

Notice that after the hypotheses are canceled, we have proved b0¬(a2=2×b2) without making any assumptions about a and b; at this stage in the proof, they are “arbitrary,” justifying the application of the universal quantifiers in the next two rules.

这个例子激发了自然演绎中的以下规则:

This example motivates the following rule in natural deduction:

假如x在任何未取消的假设中都不是自由的。这里A(x)代表任何(可能)提及的公式x. 还要记住,如果y任何“新鲜”变量都不会出现在A,我们正在考虑xA(x)等同于yA(y)

provided x is not free in any uncanceled hypothesis. Here A(x) stands for any formula that (potentially) mentions x. Also remember that if y is any “fresh” variable that does not occur in A, we are thinking of xA(x) as being the same as yA(y).

那么消去规则呢?假设我们知道每个数字都是偶数或奇数。那么,在普通的证明中,我们可以自由地断言“a是偶数或a是奇数”,或“a2是偶数或a2是奇数。”从符号逻辑上讲,这相当于以下推论:从x(even(x)odd(x)),我们可以得出结论even(t)odd(t)对于任何术语t. 这激发了全称量词的消除规则:

What about the elimination rule? Suppose we know that every number is even or odd. Then, in an ordinary proof, we are free to assert “a is even or a is odd,” or “a2 is even or a2 is odd.” In terms of symbolic logic, this amounts to the following inference: from x(even(x)odd(x)), we can conclude even(t)odd(t) for any term t. This motivates the elimination rule for the universal quantifier:

在哪里t是一个任意术语,但须遵守下一节末尾描述的限制。

where t is an arbitrary term, subject to the restriction described at the end of the next section.

从某种意义上说,这感觉就像是蕴涵的消除规则;我们可以将这个假设理解为“如果x是任何事情,那么x是偶数还是奇数。”该结论是通过将n是一个东西。请注意,一般来说,我们可以替换n语言中的任何术语,例如n(m+5)+2类似地,引言规则感觉就像蕴涵的引言规则。如果我们想证明一切都具有某种属性,我们暂时让x表示任意事物,然后表明它具有相关属性。

In a sense, this feels like the elimination rule for implication; we might read the hypothesis as saying “if x is any thing, then x is even or odd.” The conclusion is obtained by applying it to the fact that n is a thing. Note that, in general, we could replace n by any term in the language, like n(m+5)+2. Similarly, the introduction rule feels like the introduction rule for implication. If we want to show that everything has a certain property, we temporarily let x denote an arbitrary thing, and then show that it has the relevant property.

7.3.存在量词

7.3. The Existential Quantifier

与全称量词对偶的是存在量词,,用于表达诸如“某个数是偶数”或“任意两个偶数之间有一个奇数”之类的断言。

Dual to the universal quantifier is the existential quantifier, , which is used to express assertions such as “some number is even,” or, “between any two even numbers there is an odd number.”

以下关于自然数的陈述断言某个自然数的存在:

The following statements about the natural numbers assert the existence of some natural number:

  • 存在一个奇合数。(请记住,如果自然数大于 1 并且不是素数,则该自然数为合数。)

  • There exists an odd composite number. (Remember that a natural number is composite if it is greater than 1 and not prime.)

  • 每个大于一的自然数都有一个质因数。

  • Every natural number greater than one has a prime divisor.

  • 对于每一个n, 如果n素因数小于n, 然后n是复合的。

  • For every n, if n has a prime divisor smaller than n, then n is composite.

这些语句可以使用存在量词以一阶逻辑表达如下:

These statements can be expressed in first-order logic using the existential quantifier as follows:

  • n(odd(n)composite(n))

  • n(odd(n)composite(n))

  • n(n>1p(prime(p)pn))

  • n(n>1p(prime(p)pn))

  • n((p(pnprime(p)p<n))composite(n))

  • n((p(pnprime(p)p<n))composite(n))

在我们写完之后n,变量n和全称量词一样,被约束在公式中。因此公式ncomposite(n)mcomposite(m)被视为相同。

After we write n, the variable n is bound in the formula, just as for the universal quantifier. So the formulas ncomposite(n) and mcomposite(m) are considered the same.

我们如何证明这样的存在性陈述?假设我们想证明存在一个奇合数。为此,我们只需提出一个候选数,并证明该候选数满足所需的属性。例如,我们可以选择 15,然后证明 15 是奇数,15 是合数。当然,15 并没有什么特别之处,我们也可以使用其他数字(如 9 或 35)来证明这一点。选择哪个候选数并不重要,只要它具有所需的属性即可。

How do we prove such existential statements? Suppose we want to prove that there exists an odd composite number. To do this, we just present a candidate, and show that the candidate satisfies the required properties. For example, we could choose 15, and then show that 15 is odd and that 15 is composite. Of course, there’s nothing special about 15, and we could have proven it also using a different number, like 9 or 35. The choice of candidate does not matter, as long as it has the required property.

在自然演绎证明中它看起来像这样:

In a natural deduction proof this would look like this:

这说明了存在量词的引入规则:

This illustrates the introduction rule for the existential quantifier:

在哪里t是任何项,但要遵守下述限制。因此,为了证明存在公式,我们只需给出一个特定的项,我们就可以证明该公式。这样的项称为公式的见证。

where t is any term, subject to the restriction described below. So to prove an existential formula, we just have to give one particular term for which we can prove that formula. Such term is called a witness for the formula.

那么消除规则呢?假设我们知道n是某个自然数,我们知道存在一个素数p使得p<npn. 我们如何才能用这个来证明n是复合的吗?我们可以推理如下:

What about the elimination rule? Suppose that we know that n is some natural number and we know that there exists a prime p such that p<n and pn. How can we use this to prove that n is composite? We can reason as follows:

p为任意素数,使得p<npn

所以,n是复合的。

Let p be any prime such that p<n and pn.

Therefore, n is composite.

首先,我们假设p满足以下属性p是素数,p<npn,然后我们推理一下p。与使用“或”的案例推理一样,这个假设只是暂时的:如果我们能够证明n是从这个假设中合成的,我们基本上已经证明了n是复合的,假设存在这样的p。请注意,在这种推理模式中,p应该是“任意的”。换句话说,我们不应该假设p事先,我们不应该做任何额外的假设p一路走来,结论不应该提到p只有这样,我们才有理由说,结论是“仅仅”从存在一个p具有假定的属性。

First, we assume that there is some p which satisfies the properties p is prime, p<n and pn, and then we reason about that p. As with case-based reasoning using “or,” the assumption is only temporary: if we can show that n is composite from that assumption, that we have essentially shown that n is composite assuming the existence of such a p. Notice that in this pattern of reasoning, p should be “arbitrary.” In other words, we should not have assumed anything about p beforehand, we should not make any additional assumptions about p along the way, and the conclusion should not mention p. Only then does it makes sense to say that the conclusion follows from the “mere” existence of a p with the assumed properties.

在自然演绎中,消去规则表达如下:

In natural deduction, the elimination rule is expressed as follows:

这里我们要求y不是免费的B,唯一未被取消的假设是y自由发生的假设A(y)在您应用此规则时,这些将被取消。正式来说,这意味着y是“任意的”。与消除和蕴涵引入的情况一样,你可以使用假设A(y)多次证明B,并一次性取消所有。直观地看,规则表明你可以证明B从假设xA(x)通过假设A(y)对于新的变量y,并得出结论,经过任意数量的步骤,B如下。您应该将此规则与或消除规则进行比较,这有点类似。

Here we require that y is not free in B, and that the only uncanceled hypotheses where y occurs freely are the hypotheses A(y) that are canceled when you apply this rule. Formally, this is what it means to say that y is “arbitrary.” As was the case for or elimination and implication introduction, you can use the hypothesis A(y) multiple times in the proof of B, and cancel all of them at once. Intuitively, the rule says that you can prove B from the assumption xA(x) by assuming A(y) for a fresh variable y, and concluding, in any number of steps, that B follows. You should compare this rule to the rule for or elimination, which is somewhat analogous.

有期限限制t出现在全称量词的消除规则和存在量词的引入规则中的变量,即没有出现在t当你插入它时就会被绑定x。要了解违反此限制会发生什么问题,请考虑以下句子xyy>x。如果我们将其解释为关于自然数的陈述,它表示对于每个数字x,有一个更大的数字y。这是一个真实的陈述,因此无论我们用什么来代替,它都应该成立x.但是如果我们用y+1? 我们得到了声明yy>y+1,这是错误的。问题是在替换之前,变量yy+1指的是一个任意的数字,但是替换之后,它指的是存在量词断言存在的数字,而这并不是我们想要的。

There is a restriction on the term t that appears in the elimination rule for the universal quantifier and the introduction rule for the existential quantifier, namely, that no variable that appears in t becomes bound when you plug it in for x. To see what can go wrong if you violate this restriction, consider the sentence xyy>x. If we interpret this as a statement about the natural numbers, it says that for every number x, there is a bigger number y. This is a true statement, and so it should hold whatever we substitute for x. But what happens if we substitute y+1? We get the statement yy>y+1, which is false. The problem is that before the substitution the variable y in y+1 refers to an arbitrary number, but after the substitution, it refers to the number that is asserted to exist by the existential quantifier, and that is not what we want.

违反存在量词引入规则中的限制会导致类似的问题。例如,它允许我们推导出xyy=x,根据假设,它表示只有一个数字yy=y好消息是,如果你依靠直觉,你就不太可能犯这样的错误。但一个重要的事实是,自然推理的规则可以得到精确的规范,从而排除这些无效的推理。

Violating the restriction in the introduction rule for the existential quantifier causes similar problems. For example, it allows us to derive xyy=x, which says that there is exactly one number, from the hypothesis yy=y. The good news is that if you rely on your intuition, you are unlikely to make mistakes like these. But it is an important fact that the rules of natural deduction can be given a precise specification that rules out these invalid inferences.

7.4.相对化和排序

7.4. Relativization and Sorts

在我们提出的一阶逻辑中,存在一个预期的话语对象“宇宙”,而全称量词和存在量词则涵盖了该宇宙。例如,我们可以设计一种语言来谈论居住在某个城镇的人,并有一个关系loves(x,y)表达xy在这样的语言中,我们可以这样表达“每个人都爱某个人”这句话:xyloves(x,y)

In first-order logic as we have presented it, there is one intended “universe” of objects of discourse, and the universal and existential quantifiers range over that universe. For example, we could design a language to talk about people living in a certain town, with a relation loves(x,y) to express that x loves y. In such a language, we might express the statement that “everyone loves someone” by writing xyloves(x,y).

你应该记住,在这个阶段,loves只是一个符号。我们在设计语言时考虑到了某种解释,但人们也可以将语言解释为对自然数的陈述,其中loves(x,y)意味着x小于或等于y在该解释中,

You should keep in mind that, at this stage, loves is just a symbol. We have designed the language with a certain interpretation in mind, but one could also interpret the language as making statements about the natural numbers, where loves(x,y) means that x is less than or equal to y. In that interpretation, the sentence

x,y,z(loves(x,y)loves(y,z)loves(x,z))

是正确的,尽管在最初的解释中,它对三角恋的本质做出了令人难以置信的断言。在第 10 章中,我们将阐明这一概念:一阶逻辑的演绎规则使我们能够确定在所有解释中为真的陈述,就像命题逻辑的规则使我们能够确定在所有真值赋值下为真的陈述一样。

is true, though in the original interpretation it makes an implausible claim about the nature of love triangles. In Chapter 10, we will spell out the notion that the deductive rules of first-order logic enable us to determine the statements that are true in all interpretations, just as the rules of propositional logic enable us to determine the statements that are true under all truth assignments.

回到最初的例子,假设我们想要表达这样一个观点:在我们镇上,所有的女人都很强壮,所有的男人都很帅气。我们可以用以下两个句子来表达:

Returning to the original example, suppose we want to represent the statement that, in our town, all the women are strong and all the men are good looking. We could do that with the following two sentences:

  • x(woman(x)strong(x))

  • x(woman(x)strong(x))

  • x(man(x)good-looking(x))

  • x(man(x)good-looking(x))

这些都是相对化的例子。全称量词涵盖了镇上的所有人,但这种手法让我们能够使用蕴涵将我们的陈述范围分别限制在男性和女性。当我们将“大于 2 的每个素数都是奇数”表示出来时,这个技巧也派上了用场:

These are instances of relativization. The universal quantifier ranges over all the people in the town, but this device gives us a way of using implication to restrict the scope of our statements to men and women, respectively. The trick also comes into play when we render “every prime number greater than two is odd”:

x(prime(x)x>2odd(x)).

我们也可以更直白地理解为“对于每一个数字x, 如果x是素数且x大于 2,则x是奇数”,但把它读作限制量词也是很自然的。

We could also read this more literally as saying “for every number x, if x is prime and x is greater than to 2, then x is odd,” but it is natural to read it as a restricted quantifier.

也可以将存在量词相对化,以表示“某个女人很坚强”和“某个男人很帅气”之类的事情。这些表达如下:

It is also possible to relativize the existential quantifier to say things like “some woman is strong” and “some man is good-looking.” These are expressed as follows:

  • x(woman(x)strong(x))

  • x(woman(x)strong(x))

  • x(man(x)good-looking(x))

  • x(man(x)good-looking(x))

请注意,虽然我们使用蕴涵来相对化全称量词,但这里我们需要使用合取而不是蕴涵。表达式x(woman(x)strong(x))说有某种东西具有这样的属性:如果它是女人,那么它就是强壮的。从经典上讲,这相当于说有某种东西要么不是女人,要么就是强壮的,这话说起来很有趣。

Notice that although we used implication to relativize the universal quantifier, here we need to use conjunction instead of implication. The expression x(woman(x)strong(x)) says that there is something with the property that if it is a woman, then it is strong. Classically this is equivalent to saying that there is something which is either not a woman or is strong, which is a funny thing to say.

现在,假设我们正在学习几何,我们想要表达这样一个事实:给定任意两个不同的点pq以及任意两条线LM, 如果LM都通过pq,那么它们必须相同。(换句话说,两个不同点之间最多有一条线。)一种选择是设计一个一阶逻辑,其中预期的宇宙足够大,可以同时包含点和线,并使用相对化:

Now, suppose we are studying geometry, and we want to express the fact that given any two distinct points p and q and any two lines L and M, if L and M both pass through p and q, then they have to be the same. (In other words, there is at most one line between two distinct points.) One option is to design a first-order logic where the intended universe is big enough to include both points and lines, and use relativization:

p,q,L,M(point(p)point(q)line(L)line(M)qpon(p,L)on(q,L)on(p,M)on(q,M)L=M).

但处理这样的谓词是繁琐的,并且有一阶逻辑的温和扩展,称为多排序一阶逻辑,它内置了一些簿记。在多排序逻辑中,可以有不同类型的对象(例如点和线),以及针对每个对象的单独变量和量词库。此外,函数符号和谓词符号的规范表明它们期望什么类型的参数,以及在函数符号的情况下,它们返回什么类型的参数。例如,我们可以选择使用变量进行排序p,q,r,遍历点,对变量进行排序L,M,N,跨越线条,以及关系on(p,L)将两者联系起来。那么上面的断言可以更简单地表达如下:

But dealing with such predicates is tedious, and there is a mild extension of first-order logic, called many-sorted first-order logic, which builds in some of the bookkeeping. In many-sorted logic, one can have different sorts of objects—such as points and lines—and a separate stock of variables and quantifiers ranging over each. Moreover, the specification of function symbols and predicate symbols indicates what sorts of arguments they expect, and, in the case of function symbols, what sort of argument they return. For example, we might choose to have a sort with variables p,q,r, ranging over points, a sort with variables L,M,N, ranging over lines, and a relation on(p,L) relating the two. Then the assertion above is rendered more simply as follows:

p,q,L,M(pqon(p,L)on(q,L)on(p,M)on(q,M)L=M).

7.5.平等

7.5. Equality

在符号逻辑中,我们使用表达式s=t表达这一事实st是“相等”或“相同”。等号是为了模拟我们说“爱丽丝的兄弟是受害者”或“2 + 2 = 4”时的意思。我们断言两个不同的描述指的是同一个对象。因为身份的概念几乎可以应用于任何对象领域,所以它被视为属于逻辑领域。

In symbolic logic, we use the expression s=t to express the fact that s and t are “equal” or “identical.” The equality symbol is meant to model what we mean when we say, for example, “Alice’s brother is the victim,” or “2 + 2 = 4.” We are asserting that two different descriptions refer to the same object. Because the notion of identity can be applied to virtually any domain of objects, it is viewed as falling under the province of logic.

然而,谈论“平等”或“身份”会引发复杂的哲学问题。我和三天前的我还是同一个人吗?我书架上的两本《哈克贝利·费恩历险记》是同一本书,还是两本不同的书?使用符号逻辑来模拟身份的前提是,我们心中有一种划分和解释世界的方式。我们假设我们的术语指的是不同的实体,而写作s=t断言这两个表达式指的是同一个东西。公理上,我们假设相等满足以下三个属性:

Talk of “equality” or “identity” raises messy philosophical questions, however. Am I the same person I was three days ago? Are the two copies of Huckleberry Finn sitting on my shelf the same book, or two different books? Using symbolic logic to model identity presupposes that we have in mind a certain way of carving up and interpreting the world. We assume that our terms refer to distinct entities, and writing s=t asserts that the two expressions refer to the same thing. Axiomatically, we assume that equality satisfies the following three properties:

  • 反身性t=t,对于任何项t

  • reflexivity: t=t, for any term t

  • 对称性:如果s=t, 然后t=s

  • symmetry: if s=t, then t=s

  • 传递性:如果r=ss=t, 然后r=t

  • transitivity: if r=s and s=t, then r=t

然而,这些性质不足以表征相等性。如果两个表达式表示相同的事物,那么我们应该能够用一个表达式替换任何表达式中的其他表达式。采用以下约定很方便:如果r是任意项,我们可以写成r(x)表明变量x可能发生r.那么,如果s是另一个术语,我们可以这样写r(s)表示替换的结果s为了xr。因此,术语的替换规则如下:如果s=t, 然后r(s)=r(t)

These properties are not enough to characterize equality, however. If two expressions denote the same thing, then we should be able to substitute one for any other in any expression. It is convenient to adopt the following convention: if r is any term, we may write r(x) to indicate that the variable x may occur in r. Then, if s is another term, we can thereafter write r(s) to denote the result of replacing s for x in r. The substitution rule for terms thus reads as follows: if s=t, then r(s)=r(t).

我们已经对公式采用了类似的惯例:如果我们引入一个公式A(x), 然后A(t)表示代入的结果t为了xA考虑到这一点,我们可以将相等规则写如下:

We already adopted a similar convention for formulas: if we introduce a formula as A(x), then A(t) denotes the result of substituting t for x in A. With this in mind, we can write the rules for equality as follows:

这里,第一条替换规则控制项,第二条替换规则控制公式。在下一章中,您将学习如何使用它们。

Here, the first substitution rule governs terms and the second substitution rule governs formulas. In the next chapter, you will learn how to use them.

使用相等性,我们可以定义更多的量词。

Using equality, we can define even more quantifiers.

  • 我们可以表达“至少有两个元素x使得A(x)持有”为xy(xyA(x)A(y))

  • We can express “there are at least two elements x such that A(x) holds” as xy(xyA(x)A(y)).

  • 我们可以表达“最多有两个元素x使得A(x)持有”为xyz(A(x)A(y)A(z)x=yy=zx=z)这表明如果我们有三个元素a为此A(a)成立,那么其中两个一定相等。

  • We can express “there are at most two elements x such that A(x) holds” as xyz(A(x)A(y)A(z)x=yy=zx=z). This states that if we have three elements a for which A(a) holds, then two of them must be equal.

  • 我们可以表达“恰好有两个元素x使得A(x)成立”作为上述两个语句的合取。

  • We can express “there are exactly two elements x such that A(x) holds” as the conjunction of the above two statements.

作为练习,用一阶逻辑写出至少、最多和恰好三个元素的陈述x使得A(x)持有。

As an exercise, write out in first order logic the statements that there are at least, at most, and exactly three elements x such that A(x) holds.

在逻辑上,表达式!xA(x)用于表达存在一个独特的 x令人满意A(x)也就是说,只有一个这样的x如上所述,这可以表达如下:

In logic, the expression !xA(x) is used to express the fact that there is a unique x satisfying A(x), which is to say, there is exactly one such x. As above, this can be expressed as follows:

xA(x)yy(A(y)A(y)y=y).

第一个合取词表示至少有一个对象满足A,第二个连词表示最多有一个。同样的事情可以更简洁地表达如下:

The first conjunct says that there is at least one object satisfying A, and the second conjunct says that there is at most one. The same thing can be expressed more concisely as follows:

x(A(x)y(A(y)y=x)).

你应该思考一下为什么第二个表达式有效。在下一章中,我们将看到,使用自然演绎规则,我们可以证明这两个表达式是等价的。

You should think about why this second expression works. In the next chapter we will see that, using the rules of natural deduction, we can prove that these two expressions are equivalent.

7.6.练习

7.6. Exercises

  1. 完美数是指等于其真因数之和的数,真因数是指除其本身之外的除其本身以外的所有数。例如,6 是完美的,因为6=1+2+3

    使用具有自然数变量和适当函数及谓词的语言,写下断言以下内容的一阶句子。使用谓词perfect来表达一个数字是完美的。

    1. 28 是完美的。

    2. 100 至 200 之间没有完美数字。

    3. 200 到 10,000 之间至少有两个完美数字。(可以用完美数字来表达这一点xy200 至 10,000 之间,具有以下属性xy

    4. 每个完美数都是偶数。

    5. 对于每个数字,都有一个比它更大的完美数字。(这是表达“有无数个完美数字”的一种方式。)

    这里,“之间”一词ab”旨在包括ab

    顺便说一句,我们不知道最后两个说法是否正确。这些都是悬而未决的问题。

  2. A perfect number is a number that is equal to the sum of its proper divisors, that is, the numbers that divide it, other than itself. For example, 6 is perfect, because 6=1+2+3.

    Using a language with variables ranging over the natural numbers and suitable functions and predicates, write down first-order sentences asserting the following. Use a predicate perfect to express that a number is perfect.

    1. 28 is perfect.

    2. There are no perfect numbers between 100 and 200.

    3. There are (at least) two perfect numbers between 200 and 10,000. (Express this by saying that there are perfect numbers x and y between 200 and 10,000, with the property that xy.)

    4. Every perfect number is even.

    5. For every number, there is a perfect number that is larger than it. (This is one way to express the statement that there are infinitely many perfect numbers.)

    Here, the phrase “between a and b” is meant to include a and b.

    By the way, we do not know whether the last two statements are true. They are open questions.

  3. 使用包含跨人群变量和谓词的语言trusts(x,y)politician(x)crazy(x)knows(x,y)related-to(x,y), 和rich(x),写下断言以下内容的一阶句子:

    1. 没有人信任政客。

    2. 任何信任政客的人都是疯子。

    3. 每个人都认识某个与某位政治家有关系的人。

    4. 每个富人要么是政客,要么认识政客。

    每种情况都可能涉及一些解释。请注意,写下合乎逻辑的表达式是帮助澄清含义的一种方法。

  4. Using a language with variables ranging over people, and predicates trusts(x,y), politician(x), crazy(x), knows(x,y), related-to(x,y), and rich(x), write down first-order sentences asserting the following:

    1. Nobody trusts a politician.

    2. Anyone who trusts a politician is crazy.

    3. Everyone knows someone who is related to a politician.

    4. Everyone who is rich is either a politician or knows a politician.

    In each case, some interpretation may be involved. Notice that writing down a logical expression is one way of helping to clarify the meaning.

8.一阶逻辑的自然演绎

8. Natural Deduction for First Order Logic

8.1.推理规则

8.1. Rules of Inference

在上一章中,我们讨论了一阶逻辑的语言及其使用规则。我们在这里总结一下:

In the last chapter, we discussed the language of first-order logic, and the rules that govern their use. We summarize them here:

全称量词:

The universal quantifier:

在介绍规则中,x在任何未取消的假设中都不应该自由。在消除规则中,t可以是任何不与任何绑定变量冲突的术语A

In the introduction rule, x should not be free in any uncanceled hypothesis. In the elimination rule, t can be any term that does not clash with any of the bound variables in A.

存在量词:

The existential quantifier:

在介绍规则中,t可以是任何不与任何绑定变量冲突的术语A在淘汰规则中,y不应该免费B或任何未被取消的假设。

In the introduction rule, t can be any term that does not clash with any of the bound variables in A. In the elimination rule, y should not be free in B or any uncanceled hypothesis.

平等:

Equality:

严格来说,只有refl和第二条替换规则是必需的。其他规则可以从它们中推导出来。

Strictly speaking, only refl and the second substitution rule are necessary. The others can be derived from them.

8.2.通用量词

8.2. The Universal Quantifier

以下自然演绎证明示例表明,如果对于每个xA(x)成立,并且对于每一个xB(x)成立,那么对于每一个x,它们都成立:

The following example of a proof in natural deduction shows that if, for every x, A(x) holds, and for every x, B(x) holds, then for every x, they both hold:

请注意,假设 1 和假设 2 都没有提到y, 以便y在引入全称量词时,它实际上是“任意的”。

Notice that neither of the assumptions 1 or 2 mention y, so that y is really “arbitrary” at the point where the universal quantifiers are introduced.

下面是另一个示例:

Here is another example:

作为练习,尝试证明以下内容:

As an exercise, try proving the following:

x(A(x)B(x))(xA(x)xB(x)).

这是一个更具挑战性的练习。假设我告诉你,在一个城镇里,有一位(男性)理发师,他只为所有不自己刮胡子的男士刮胡子。你可以证明这是一个矛盾,非正式的论证如下:

Here is a more challenging exercise. Suppose I tell you that, in a town, there is a (male) barber that shaves all and only the men who do not shave themselves. You can show that this is a contradiction, arguing informally, as follows:

根据假设,理发师给自己刮胡子当且仅当他不给自己刮胡子。将此语句称为 (*)。

假设理发师给自己刮胡子。根据 (*),这意味着他不给自己刮胡子,这是一个矛盾。所以,理发师不给自己刮胡子。

但是再次使用 (*),这意味着理发师给自己刮胡子,这与我们刚才证明的事实相矛盾,即理发师不给自己刮胡子。

By the assumption, the barber shaves himself if and only if he does not shave himself. Call this statement (*).

Suppose the barber shaves himself. By (*), this implies that he does not shave himself, a contradiction. So, the barber does not shave himself.

But using (*) again, this implies that the barber shaves himself, which contradicts the fact we just showed, namely, that the barber does not shave himself.

尝试将其转化为自然推理的正式论证。

Try to turn this into a formal argument in natural deduction.

让我们回到自然数的例子,看看演绎概念是如何发挥作用的。假设我们定义了evenodd这样我们就可以证明:

Let us return to the example of the natural numbers, to see how deductive notions play out there. Suppose we have defined even and odd in such a way that we can prove:

  • n(¬even(n)odd(n))

  • n(¬even(n)odd(n))

  • n(odd(n)¬even(n))

  • n(odd(n)¬even(n))

然后我们可以继续推导n(even(n)odd(n))如下:

Then we can go on to derive n(even(n)odd(n)) as follows:

我们还可以证明n¬(even(n)odd(n))

We can also prove and n¬(even(n)odd(n)):

随着我们从建模基本推理规则转向建模实际数学证明,我们将倾向于将重点从自然演绎转向 Lean 中的形式化证明。自然演绎有其用途:作为逻辑推理的模型,它为我们提供了一种研究元理论属性(如健全性和完整性)的便捷方法。然而,对于系统内工作,像 Lean 这样的证明语言往往具有更好的可扩展性,并能产生更易读的证明。

As we move from modeling basic rules of inference to modeling actual mathematical proofs, we will tend to shift focus from natural deduction to formal proofs in Lean. Natural deduction has its uses: as a model of logical reasoning, it provides us with a convenient means to study metatheoretic properties such as soundness and completeness. For working within the system, however, proof languages like Lean’s tend to scale better, and produce more readable proofs.

8.3.存在量词

8.3. The Existential Quantifier

请记住,存在量词消除规则背后的直觉是,如果我们知道xA(x),我们可以暂时推理任意元素y令人满意A(y)为了证明一个不依赖于y。下面是如何使用它的一个例子。下一个证明说,如果我们知道有一些东西可以同时满足AB那么我们就知道,有一些令人满意的事情A

Remember that the intuition behind the elimination rule for the existential quantifier is that if we know xA(x), we can temporarily reason about an arbitrary element y satisfying A(y) in order to prove a conclusion that doesn’t depend on y. Here is an example of how it can be used. The next proof says that if we know there is something satisfying both A and B, then we know, in particular, that there is something satisfying A.

以下证明表明,如果存在满足A或者B,那么要么有令人满意的东西A或有令人满意的事情B

The following proof shows that if there is something satisfying either A or B, then either there is something satisfying A, or there is something satisfying B.

下面的例子更加复杂:

The following example is more involved:

在这个证明中,存在消除规则(标有3) 用于同时取消两个假设。请注意,应用此规则时,假设x(A(x)¬B(x))尚未取消。因此我们必须确保此公式不包含变量x自由。但这是可以的,因为这个假设包含x仅作为绑定变量。

In this proof, the existential elimination rule (the line labeled 3) is used to cancel two hypotheses at the same time. Note that when this rule is applied, the hypothesis x(A(x)¬B(x)) has not yet been canceled. So we have to make sure that this formula doesn’t contain the variable x freely. But this is o.k., since this hypothesis contains x only as a bound variable.

另一个例子是x不会发生在P, 然后xP相当于P

Another example is that if x does not occur in P, then xP is equivalent to P:

这很短但很棘手,所以让我们仔细研究一下。在左边,我们假设xP总结P.我们假设P,现在我们可以通过存在消去立即取消这个假设,因为x不会发生在P,因此它不会自由地出现在任何假设或结论中。在右边,我们使用存在性引言来得出结论xPP

This is short but tricky, so let us go through it carefully. On the left, we assume xP to conclude P. We assume P, and now we can immediately cancel this assumption by existential elimination, since x does not occur in P, so it doesn’t occur freely in any assumption or in the conclusion. On the right we use existential introduction to conclude xP from P.

8.4.平等

8.4. Equality

回想一下平等的自然演绎规则:

Recall the natural deduction rules for equality:

请记住,我们已经隐式地固定了一些一阶语言,并且rs, 和t是该语言中的任何术语。还记得,我们已经采用了对术语使用函数符号的做法。例如,如果我们想到r(x)作为术语(x+y)×(z+0)用算术语言来说,r(0)是术语(0+y)×(z+0)r(u+v)((u+v)+y)×(z+0)。因此,第二行第一个推论的一个例子是这样的:

Keep in mind that we have implicitly fixed some first-order language, and r, s, and t are any terms in that language. Recall also that we have adopted the practice of using functional notation with terms. For example, if we think of r(x) as the term (x+y)×(z+0) in the language of arithmetic, then r(0) is the term (0+y)×(z+0) and r(u+v) is ((u+v)+y)×(z+0). So one example of the first inference on the second line is this:

这条线上的第二个公理是类似的,只不过现在P(x)代表任何公式,如下推论:

The second axiom on that line is similar, except now P(x) stands for any formula, as in the following inference:

请注意,我们已经写了反身性公理,t=t,作为没有前提的规则。如果你在证明中使用它,它不算作假设;它内置于逻辑中。

Notice that we have written the reflexivity axiom, t=t, as a rule with no premises. If you use it in a proof, it does not count as a hypothesis; it is built into the logic.

事实上,我们可以把第二行的第一个推论看作是第二个推论的一个特例。例如,考虑公式((u+v)+y)×(z+0)=(x+y)×(z+0).如果我们插入u+vx,我们得到一个反身性的实例。如果我们代入0,我们得到了上面第一个例子的结论。因此,以下是第一个推论的推导,仅使用反身性和上面的第二个替换规则:

In fact, we can think of the first inference on the second line as a special case of the second one. Consider, for example, the formula ((u+v)+y)×(z+0)=(x+y)×(z+0). If we plug u+v in for x, we get an instance of reflexivity. If we plug in 0, we get the conclusion of the first example above. The following is therefore a derivation of the first inference, using only reflexivity and the second substitution rule above:

粗略地说,我们正在替换第二个实例u+v在反身性实例中0得到我们想要的结论。

Roughly speaking, we are replacing the second instance of u+v in an instance of reflexivity with 0 to get the conclusion we want.

等式规则让我们能够用符号逻辑进行计算。这通常相当于使用我们已经讨论过的等式规则以及一系列一般恒等式。例如,以下恒等式适用于任何实数xy, 和z

Equality rules let us carry out calculations in symbolic logic. This typically amounts to using the equality rules we have already discussed, together with a list of general identities. For example, the following identities hold for any real numbers x, y, and z:

  • 加法的交换性:x+y=y+x

  • commutativity of addition: x+y=y+x

  • 加法的结合律:(x+y)+z=x+(y+z)

  • associativity of addition: (x+y)+z=x+(y+z)

  • 加法恒等式:x+0=0+x=x

  • additive identity: x+0=0+x=x

  • 加法逆元:x+x=x+x=0

  • additive inverse: x+x=x+x=0

  • 乘法恒等式:x1=1x=x

  • multiplicative identity: x1=1x=x

  • 乘法交换性:xy=yx

  • commutativity of multiplication: xy=yx

  • 乘法的结合律:(xy)z=x(yz)

  • associativity of multiplication: (xy)z=x(yz)

  • 分配性:x(y+z)=xy+xz,(x+y)z=xz+yz

  • distributivity: x(y+z)=xy+xz,(x+y)z=xz+yz

你应该想象每个语句前面都有隐式的全称量词,断言该语句对任何值都成立xy, 和z。 注意xy, 和z具体来说,可以是整数或有理数。涉及实数、有理数或整数的计算通常涉及这样的恒等式。

You should imagine that there are implicit universal quantifiers in front of each statement, asserting that the statement holds for any values of x, y, and z. Note that x, y, and z can, in particular, be integers or rational numbers as well. Calculations involving real numbers, rational numbers, or integers generally involve identities like this.

该策略是使用全称量词的消去规则来实例化一般恒等式,在必要时使用对称性将方程导向正确的方向,然后使用等式的替换规则来改变先前结果中的某些内容。例如,这是一个简单恒等式的自然演绎证明,x,y,z((x+y)+z=(x+z)+y),仅使用加法的交换律和结合律。我们擅自使用了一个简短的名称来表示相关身份,并将全称量词引入和消除规则的多个实例合并为一个步骤。

The strategy is to use the elimination rule for the universal quantifier to instantiate general identities, use symmetry, if necessary, to orient an equation in the right direction, and then using the substitution rule for equality to change something in a previous result. For example, here is a natural deduction proof of a simple identity, x,y,z((x+y)+z=(x+z)+y), using only commutativity and associativity of addition. We have taken the liberty of using a brief name to denote the relevant identities, and combining multiple instances of the universal quantifier introduction and elimination rules into a single step.

通常,通过自然演绎进行此类计算不会学到什么有趣的东西,但你应该尝试一两个例子来掌握它,然后高兴地知道它是可能的。

There is generally nothing interesting to be learned from carrying out such calculations in natural deduction, but you should try one or two examples to get the hang of it, and then take pleasure in knowing that it is possible.

8.5.反例和相对量词

8.5. Counterexamples and Relativized Quantifiers

考虑以下陈述:

Consider the statement:

每个质数都是奇数。

Every prime number is odd.

在一阶逻辑中,我们可以将其表述为p(prime(p)odd(p))。这个命题是错误的,因为有一个素数是偶数,即2。这被称为这个命题的反例。

In first-order logic, we could formulate this as p(prime(p)odd(p)). This statement is false, because there is a prime number that is even, namely the number 2. This is called a counterexample to the statement.

更一般地,给定一个公式xA(x),反例是一个值t使得¬A(t)成立。这样的反例表明原公式是错误的,因为我们有以下等价性:¬xA(x)x¬A(x).因此如果我们找到一个值t使得¬A(t)成立,那么根据存在引入规则,我们可以得出结论x¬A(x),然后根据上述等价关系,我们得到¬xA(x)。下面是等价性的证明:

More generally, given a formula xA(x), a counterexample is a value t such that ¬A(t) holds. Such a counterexample shows that the original formula is false, because we have the following equivalence: ¬xA(x)x¬A(x). So if we find a value t such that ¬A(t) holds, then by the existential introduction rule we can conclude that x¬A(x), and then by the above equivalence we have ¬xA(x). Here is a proof of the equivalence:

关于证明的一个注释:在标记为4我们不能使用存在性引入规则,因为那时我们唯一的假设是¬xA(x),并且根据这个假设我们无法证明¬A(t)对于特定术语t。所以我们在这里使用矛盾证明。

One remark about the proof: at the step marked by 4 we cannot use the existential introduction rule, because at that point our only assumption is ¬xA(x), and from that assumption we cannot prove ¬A(t) for a particular term t. So we use a proof by contradiction there.

作为练习,自己证明“对偶”等价性:¬xA(x)x¬A(x).无需使用反证法即可实现这一点。

As an exercise, prove the “dual” equivalence yourself: ¬xA(x)x¬A(x). This can be done without using proof by contradiction.

在第 7 章中,我们看到了如何使用相对化来限制全称量词的范围的例子。假设我们想说“每个素数都大于 1”。在一阶逻辑中,这可以写成n(prime(n)n>1)原因是,原语句等价于“对于每个自然数,如果它是素数,那么它大于 1”这个语句。同样,假设我们要说“存在一个大于 100 的素数”。这相当于说“存在一个大于 100 的素数”,可以表示为n(prime(n)n>100)

In Chapter 7 we saw examples of how to use relativization to restrict the scope of a universal quantifier. Suppose we want to say “every prime number is greater than 1”. In first order logic this can be written as n(prime(n)n>1). The reason is that the original statement is equivalent to the statement “for every natural number, if it is prime, then it is greater than 1”. Similarly, suppose we want to say “there exists a prime number greater than 100.” This is equivalent to saying “there exists a natural number which is prime and greater than 100,” which can be expressed as n(prime(n)n>100).

作为练习,你可以证明上述关于量词否定的结果也适用于相对量词。具体来说,证明以下陈述:

As an exercise you can prove the above results about negations of quantifiers also for relativized quantifiers. Specifically, prove the following statements:

  • ¬x(A(x)B(x))x(A(x)¬B(x))

  • ¬x(A(x)B(x))x(A(x)¬B(x))

  • ¬x(A(x)B(x))x(A(x)¬B(x))

  • ¬x(A(x)B(x))x(A(x)¬B(x))

作为参考,下面列出了涉及量词的有效句子:

For reference, here is a list of valid sentences involving quantifiers:

  • xAA如果x不是免费的A

  • xAA if x is not free in A

  • xAA如果x不是免费的A

  • xAA if x is not free in A

  • x(A(x)B(x))xA(x)xB(x)

  • x(A(x)B(x))xA(x)xB(x)

  • x(A(x)B)xA(x)B如果x不是免费的B

  • x(A(x)B)xA(x)B if x is not free in B

  • x(A(x)B(x))xA(x)xB(x)

  • x(A(x)B(x))xA(x)xB(x)

  • x(A(x)B)xA(x)B如果x不是免费的B

  • x(A(x)B)xA(x)B if x is not free in B

  • x(A(x)B)(xA(x)B)如果x不是免费的B

  • x(A(x)B)(xA(x)B) if x is not free in B

  • x(A(x)B)(xA(x)B)如果x不是免费的B

  • x(A(x)B)(xA(x)B) if x is not free in B

  • x(AB(x))(AxB(x))如果x不是免费的A

  • x(AB(x))(AxB(x)) if x is not free in A

  • x(A(x)B)(A(x)xB)如果x不是免费的B

  • x(A(x)B)(A(x)xB) if x is not free in B

  • xA(x)¬x¬A(x)

  • xA(x)¬x¬A(x)

  • xA(x)¬x¬A(x)

  • xA(x)¬x¬A(x)

  • ¬xA(x)x¬A(x)

  • ¬xA(x)x¬A(x)

  • ¬xA(x)x¬A(x)

  • ¬xA(x)x¬A(x)

所有这些都可以通过自然演绎推导出来。最后两个规则允许我们将否定推向内部,因此我们可以继续将一阶公式置于否定范式中。其他规则允许我们将量词放在任何公式的前面,尽管一般来说,这样做有多种方法。例如,公式

All of these can be derived in natural deduction. The last two allow us to push negations inwards, so we can continue to put first-order formulas in negation normal form. Other rules allow us to bring quantifiers to the front of any formula, though, in general, there will be multiple ways of doing this. For example, the formula

xA(x)yzB(y,z)

相当于两者

is equivalent to both

x,yz(A(x)B(y,z))

and

yzx(A(x)B(y,z)).

所有量词都在前面的公式被称为前缀形式。

A formula with all the quantifiers in front is said to be in prenex form.

8.6.练习

8.6. Exercises

  1. 给出自然演绎证明

    x(A(x)B(x))(xA(x)xB(x)).
  2. Give a natural deduction proof of

    x(A(x)B(x))(xA(x)xB(x)).
  3. 给出自然演绎证明xB(x)来自假设x(A(x)B(x))y¬A(y)

  4. Give a natural deduction proof of xB(x) from hypotheses x(A(x)B(x)) and y¬A(y).

  5. 从假设出发x(even(x)odd(x))x(odd(x)even(s(x)))给出自然演绎证明x(even(x)even(s(x)))(想想s(x)定义为函数s(x)=x+1

  6. From hypotheses x(even(x)odd(x)) and x(odd(x)even(s(x))) give a natural deduction proof x(even(x)even(s(x))). (It might help to think of s(x) as the function defined by s(x)=x+1.)

  7. 给出自然演绎证明xA(x)xB(x)x(A(x)B(x))

  8. Give a natural deduction proof of xA(x)xB(x)x(A(x)B(x)).

  9. 给出自然演绎证明x(A(x)C(x))根据假设x(A(x)B(x))x(A(x)B(x)C(x))

  10. Give a natural deduction proof of x(A(x)C(x)) from the assumptions x(A(x)B(x)) and x(A(x)B(x)C(x)).

  11. 证明最后一节中的其他一些等价性。

  12. Prove some of the other equivalences in the last section.

  13. 考虑一下一阶逻辑中表达“没有人相信政治家”的各种方式:

    • x(politician(x)y(¬trusts(y,x)))

    • x,y(politician(x)¬trusts(y,x))

    • ¬x,y(politician(x)trusts(y,x))

    • x,y(trusts(y,x)¬politician(x))

    它们都是逻辑等价的。通过对每个命题给出自然演绎证明,来证明第二命题和第四命题。(作为一种捷径,在引入和消除规则,您可以在一个步骤中引入/消除两个变量。)

  14. Consider some of the various ways of expressing “nobody trusts a politician” in first-order logic:

    • x(politician(x)y(¬trusts(y,x)))

    • x,y(politician(x)¬trusts(y,x))

    • ¬x,y(politician(x)trusts(y,x))

    • x,y(trusts(y,x)¬politician(x))

    They are all logically equivalent. Show this for the second and the fourth, by giving natural deduction proofs of each from the other. (As a shortcut, in the introduction and elimination rules, you can introduce / eliminate both variables in one step.)

  15. 将以下陈述形式化,并给出自然的演绎证明,其中前三个陈述作为(未取消的)假设出现,最后一行是结论:

    • 每个年轻健康的人都喜欢棒球。

    • 每一个活跃的人都是健康的。

    • 有人年轻,活跃。

    • 因此,有人喜欢棒球。

    使用Y(x)因为“年轻”,H(x)对于“健康”A(x)对于“处于活动状态”,B(x)意思是“喜欢棒球”。

  16. Formalize the following statements, and give a natural deduction proof in which the first three statements appear as (uncancelled) hypotheses, and the last line is the conclusion:

    • Every young and healthy person likes baseball.

    • Every active person is healthy.

    • Someone is young and active.

    • Therefore, someone likes baseball.

    Use Y(x) for “is young,” H(x) for “is healthy,” A(x) for “is active,” and B(x) for “likes baseball.”

  17. 给出自然演绎证明x,y,z(x=z(y=zx=y))使用第 8.4 节中的相等规则。

  18. Give a natural deduction proof of x,y,z(x=z(y=zx=y)) using the equality rules in Section 8.4.

  19. 给出自然演绎证明x,y(x=yy=x)仅使用这两个假设(而不使用任何新的相等规则):

    • x(x=x)

    • u,v,w(u=w(v=wu=v))

    (提示:选择实例uv, 和w小心一点。你可以像上次的家庭作业一样,一步就实例化所有全称量词。)

  20. Give a natural deduction proof of x,y(x=yy=x) using only these two hypotheses (and none of the new equality rules):

    • x(x=x)

    • u,v,w(u=w(v=wu=v))

    (Hint: Choose instantiations of u, v, and w carefully. You can instantiate all the universal quantifiers in one step, as on the last homework assignment.)

  21. 给出自然演绎证明¬x(A(x)B(x))x(A(x)¬B(x))

  22. Give a natural deduction proof of ¬x(A(x)B(x))x(A(x)¬B(x))

  23. 给出自然演绎证明¬x(A(x)B(x))x(A(x)¬B(x))

  24. Give a natural deduction proof of ¬x(A(x)B(x))x(A(x)¬B(x))

  25. 请记住,以下两种表达!xA(x)也就是说,存在一个唯一的x令人满意A(x)

    • x(A(x)y(A(y)y=x))

    • xA(x)yy(A(y)A(y)y=y)

    执行以下操作:

    • 以第一点为假设,对第二点给出自然的演绎证明。

    • 对第一个问题给出自然的演绎证明,并将第二个问题作为假设。

    (警告:这些很长。)

  26. Remember that both the following express !xA(x), that is, the statement that there is a unique x satisfying A(x):

    • x(A(x)y(A(y)y=x))

    • xA(x)yy(A(y)A(y)y=y)

    Do the following:

    • Give a natural deduction proof of the second, assuming the first as a hypothesis.

    • Give a natural deduction proof of the first, asssuming the second as a hypothesis.

    (Warning: these are long.)

9.精益中的一阶逻辑

9. First Order Logic in Lean

9.1.函数、谓词和关系

9.1. Functions, Predicates, and Relations

在上一章中,我们讨论了一阶逻辑的语言。在本书中,我们将看到 Lean 的内置逻辑更具表现力;但它包括一阶逻辑,也就是说,任何可以用一阶逻辑表达(和证明)的东西都可以用 Lean 表达(和证明)。

In the last chapter, we discussed the language of first-order logic. We will see in the course of this book that Lean’s built-in logic is much more expressive; but it includes first-order logic, which is to say, anything that can be expressed (and proved) in first-order logic can be expressed (and proved) in Lean.

精益基于一种称为类型理论的基础框架,其中每个变量都假定为某种类型的元素。从一阶逻辑的角度来看,你可以将类型视为“宇宙”或“论域”。

Lean is based on a foundational framework called type theory, in which every variable is assumed to range elements of some type. You can think of a type as being a “universe,” or a “domain of discourse,” in the sense of first-order logic.

例如,假设我们要使用具有一个常量符号、一个一元函数符号、一个二元函数符号、一个一元关系符号和一个二元关系符号的一阶语言。我们可以声明一个新类型U(表示“universe”)和相关符号,如下所示:

For example, suppose we want to work with a first-order language with one constant symbol, one unary function symbol, one binary function symbol, one unary relation symbol, and one binary relation symbol. We can declare a new type U (for “universe”) and the relevant symbols as follows:

constant U : Type

constant c : U
constant f : U  U
constant g : U  U  U
constant P : U  Prop
constant R : U  U  Prop
constant U : Type

constant c : U
constant f : U  U
constant g : U  U  U
constant P : U  Prop
constant R : U  U  Prop

然后我们可以按如下方式使用它们:

We can then use them as follows:

variables x y : U

#check c
#check f c
#check g x y
#check g x (f c)

#check P (g x (f c))
#check R x y
variables x y : U

#check c
#check f c
#check g x y
#check g x (f c)

#check P (g x (f c))
#check R x y

#check命令告诉我们前四个表达式的类型为U,后两个表达式的类型为Prop。粗略地说,这意味着前四个表达式对应于一阶逻辑的项,后两个表达式对应于公式。

The #check command tells us that the first four expressions have type U, and that the last two have type Prop. Roughly, this means that the first four expressions correspond to terms of first-order logic, and that the last two correspond to formulas.

请注意以下所有内容:

Note all the following:

  • 一元函数表示为类型的对象,二元函数表示为类型的对象,使用与命题之间的蕴涵相同的符号。U UU U U

  • A unary function is represented as an object of type U U and a binary function is represented as an object of type U U U, using the same notation as for implication between propositions.

  • 例如,我们写为表示将应用于的结果,以及表示将应用于和 的结果,就像我们在使用肯定前件进行一阶逻辑时所做的那样。表达式中需要使用括号来确保被解析为单个参数。f xfxg x ygxyg x (f c)f c

  • We write, for example, f x to denote the result of applying f to x, and g x y to denote the result of applying g to x and y, again just as we did when using modus ponens for first-order logic. Parentheses are needed in the expression g x (f c) to ensure that f c is parsed as a single argument.

  • 一元谓词表示为 类型的对象,二元谓词表示为 类型的对象。你可以将二元关系视为一个函数,它假设宇宙中的两个参数,并返回一个命题。U PropU U PropRU

  • A unary predicate is presented as an object of type U Prop and a binary predicate is represented as an object of type U U Prop. You can think of a binary relation R as being a function that assumes two arguments in the universe, U, and returns a proposition.

  • 我们写为表示对于 成立的断言,以及表示对于和成立。P xPxR x yRxy

  • We write P x to denote the assertion that P holds of x, and R x y to denote that R holds of x and y.

您可能想知道 Lean 中的常量和变量之间有什么区别。以下声明也有效:

You may reasonably wonder what difference there is between a constant and a variable in Lean. The following declarations also work:

variable U : Type

variable c : U
variable f : U  U
variable g : U  U  U
variable P : U  Prop
variable R : U  U  Prop

variables x y : U

#check c
#check f c
#check g x y
#check g x (f c)

#check P (g x (f c))
#check R x y
variable U : Type

variable c : U
variable f : U  U
variable g : U  U  U
variable P : U  Prop
variable R : U  U  Prop

variables x y : U

#check c
#check f c
#check g x y
#check g x (f c)

#check P (g x (f c))
#check R x y

尽管示例的功能大致相同,但constantvariable命令的作用却大不相同。constant命令公理地声明一个新对象,并将其添加到 Lean 知道的对象列表中。相比之下,首次执行时,命令variable不会创建任何东西。相反,它告诉 Lean,每当我们使用相应的标识符输入表达式时,它都应该创建一个相应类型的临时变量。

Although the examples function in much the same way, the constant and variable commands do very different things. The constant command declares a new object, axiomatically, and adds it to the list of objects Lean knows about. In contrast, when it is first executed, the variable command does not create anything. Rather, it tells Lean that whenever we enter an expression using the corresponding identifier, it should create a temporary variable of the corresponding type.

Lean 的标准库中已经声明了许多类型。例如,有一种类型写为nat,表示自然数:

Many types are already declared in Lean’s standard library. For example, there is a type written nat or , that denotes the natural numbers:

#check nat
#check 
#check nat
#check 

您可以使用\nat或输入unicode \N。 这两个表达式含义相同。

You can enter the unicode with \nat or \N. The two expressions mean the same thing.

使用这种内置类型,我们可以对算术语言进行建模,如上一章所述,如下所示:

Using this built-in type, we can model the language of arithmetic, as described in the last chapter, as follows:

namespace hidden

constant mul :     
constant add :     
constant square :   
constant even :   Prop
constant odd :   Prop
constant prime :   Prop
constant divides :     Prop
constant lt :     Prop
constant zero : 
constant one : 

end hidden
namespace hidden

constant mul :     
constant add :     
constant square :   
constant even :   Prop
constant odd :   Prop
constant prime :   Prop
constant divides :     Prop
constant lt :     Prop
constant zero : 
constant one : 

end hidden

我们使用了namespace命令来避免与 Lean 库中已声明的标识符发生冲突。(在命名空间之外,mul我们刚刚声明的常量名为hidden.mul。)我们可以再次使用该#check命令来尝试它们:

We have used the namespace command to avoid conflicts with identifiers that are already declared in the Lean library. (Outside the namespace, the constant mul we just declared is named hidden.mul.) We can again use the #check command to try them out:

namespace hidden

constant mul :     
constant add :     
constant square :   
constant even :   Prop
constant odd :   Prop
constant prime :   Prop
constant divides :     Prop
constant lt :     Prop
constant zero : 
constant one : 

variables w x y z : 

#check mul x y
#check add x y
#check square x
#check even x

end hidden
namespace hidden

constant mul :     
constant add :     
constant square :   
constant even :   Prop
constant odd :   Prop
constant prime :   Prop
constant divides :     Prop
constant lt :     Prop
constant zero : 
constant one : 

variables w x y z : 

#check mul x y
#check add x y
#check square x
#check even x

end hidden

我们甚至可以声明二元运算和关系的中缀表示法:

We can even declare infix notation of binary operations and relations:

local infix + := add
local infix * := mul
local infix < := lt
local infix + := add
local infix * := mul
local infix < := lt

1(获取数字、2、 、 ...的符号3比较棘手。)有了所有这些,上面的例子可以呈现如下:

(Getting notation for numerals 1, 2, 3, … is trickier.) With all this in place, the examples above can be rendered as follows:

#check even (x + y + z)  prime ((x + one) * y * y)
#check ¬ (square (x + y * z) = w)  x + y < z
#check x < y  even x  even y  x + one < y
#check even (x + y + z)  prime ((x + one) * y * y)
#check ¬ (square (x + y * z) = w)  x + y < z
#check x < y  even x  even y  x + one < y

事实上,这里讨论的所有函数、谓词和关系(除“square”函数外)都是在 Lean 库中定义的。当我们将命令和放在 Lean 中的文件顶部时,它们就可供我们使用。import data.nat.primeopen nat

In fact, all of the functions, predicates, and relations discussed here, except for the “square” function, are defined in the Lean library. They become available to us when we put the commands import data.nat.prime and open nat at the top of a file in Lean.

import data.nat.prime
open nat

constant square :   

variables w x y z : 

#check even (x + y + z)  prime ((x + 1) * y * y)
#check ¬ (square (x + y * z) = w)  x + y < z
#check x < y  even x  even y  x + 1 < y
import data.nat.prime
open nat

constant square :   

variables w x y z : 

#check even (x + y + z)  prime ((x + 1) * y * y)
#check ¬ (square (x + y * z) = w)  x + y < z
#check x < y  even x  even y  x + 1 < y

在这里,我们公理化地声明常量squareprime,但引用 Lean 库中的其他操作和谓词。在本书中,我们通常会以这种方式进行,明确告诉您应该使用库中的哪些事实进行练习。

Here, we declare the constants square and prime axiomatically, but refer to the other operations and predicates in the Lean library. In this book, we will often proceed in this way, telling you explicitly what facts from the library you should use for exercises.

再次注意语法的以下几个方面:

Again, note the following aspects of syntax:

  • 与普通数学符号不同,在精益中,函数的应用无需括号或逗号。例如,我们写成square xadd x ysquare(x)add(x,y)

  • In contrast to ordinary mathematical notation, in Lean, functions are applied without parentheses or commas. For example, we write square x and add x y instead of square(x) and add(x,y).

  • 谓词和关系也是如此:我们写成even xlt x yeven(x)lt(x,y),就像在符号逻辑中所做的那样。

  • The same holds for predicates and relations: we write even x and lt x y instead of even(x) and lt(x,y), as one might do in symbolic logic.

  • 该符号表示加法假设两个参数,都是自然数,并返回一个自然数。add :

  • The notation add : indicates that addition assumes two arguments, both natural numbers, and returns a natural number.

  • 类似地,符号表示是二元关系,它假设两个自然数作为参数并形成一个命题。换句话说,表达了除以的断言。divides : Propdividesdivides x yxy

  • Similarly, the notation divides : Prop indicates that divides is a binary relation, which assumes two natural numbers as arguments and forms a proposition. In other words, divides x y expresses the assertion that x divides y.

Lean 可以帮助我们区分术语和公式。如果我们使用Lean 中的#check表达式,我们会被告知它具有类型,也就是说,它表示一个自然数。如果我们使用表达式,我们会被告知它具有类型,也就是说,它表达了一个命题。x + y + 1#checkeven (x + y + 1)Prop

Lean can help us distinguish between terms and formulas. If we #check the expression x + y + 1 in Lean, we are told it has type , which is to say, it denotes a natural number. If we #check the expression even (x + y + 1), we are told that it has type Prop, which is to say, it expresses a proposition.

在第 7 章中,我们考虑了多类别逻辑,其中可以有多个领域。例如,我们可能希望将一阶逻辑用于几何,量词的范围包括点和线。在精益中,我们可以通过为每种类别引入一种新类型来建模:

In Chapter 7 we considered many-sorted logic, where one can have multiple universes. For example, we might want to use first-order logic for geometry, with quantifiers ranging over points and lines. In Lean, we can model this as by introducing a new type for each sort:

variables Point Line : Type
variable  lies_on : Point  Line  Prop
variables Point Line : Type
variable  lies_on : Point  Line  Prop

然后我们可以表达两个不同的点确定一条线,如下所示:

We can then express that two distinct points determine a line as follows:

#check  (p q : Point) (L M : Line),
        p  q  lies_on p L  lies_on q L  lies_on p M 
          lies_on q M  L = M
#check  (p q : Point) (L M : Line),
        p  q  lies_on p L  lies_on q L  lies_on p M 
          lies_on q M  L = M

请注意,我们遵循了在先行词中使用迭代蕴涵而不是合取的惯例。事实上,Lean 足够聪明,可以根据它们与关系一起使用的事实推断出pqL和是什么类型的对象,因此我们可以更简单地写成这样:Mlies_on

Notice that we have followed the convention of using iterated implication rather than conjunction in the antecedent. In fact, Lean is smart enough to infer what sorts of objects p, q, L, and M are from the fact that they are used with the relation lies_on, so we could have written, more simply, this:

#check  p q L M, p  q  lies_on p L  lies_on q L 
  lies_on p M  lies_on q M  L = M
#check  p q L M, p  q  lies_on p L  lies_on q L 
  lies_on p M  lies_on q M  L = M

9.2.使用全称量词

9.2. Using the Universal Quantifier

在精益中,你可以通过 来输入全称量词。第 7.1 节\all中的激励示例如下所示:

In Lean, you can enter the universal quantifier by writing \all. The motivating examples from Section 7.1 are rendered as follows:

import data.nat.prime
open nat

#check  x, (even x  odd x)  ¬ (even x  odd x)
#check  x, even x  2  x
#check  x, even x  even (x^2)
#check  x, even x  odd (x + 1)
#check  x, prime x  x > 2  odd x
#check  x y z, x  y  y  z  x  z
import data.nat.prime
open nat

#check  x, (even x  odd x)  ¬ (even x  odd x)
#check  x, even x  2  x
#check  x, even x  even (x^2)
#check  x, even x  odd (x + 1)
#check  x, prime x  x > 2  odd x
#check  x y z, x  y  y  z  x  z

请记住,Lean 要求全称量词后面有一个逗号,并赋予其尽可能宽的范围。例如,被解释为,我们会写成来限制范围。如果您愿意,可以使用纯 ascii 表达式代替 unicode 。 x, P Q x, (P Q)(∀ x, P) Qforall

Remember that Lean expects a comma after the universal quantifier, and gives it the widest scope possible. For example, x, P Q is interpreted as x, (P Q), and we would write (∀ x, P) Q to limit the scope. If you prefer, you can use the plain ascii expression forall instead of the unicode .

在精益中,证明通用陈述的模式如下:

In Lean, then, the pattern for proving a universal statement is rendered as follows:

variable U : Type
variable P : U  Prop

example :  x, P x :=
assume x,
show P x, from sorry
variable U : Type
variable P : U  Prop

example :  x, P x :=
assume x,
show P x, from sorry

读作“固定的任意值”。由于我们可以随意重命名绑定变量,因此我们可以等效地编写以下任一代码:assume xxU

Read assume x as “fix an arbitrary value x of U.” Since we are allowed to rename bound variables at will, we can equivalently write either of the following:

variable U : Type
variable P : U  Prop

example :  y, P y :=
assume x,
show P x, from sorry

example :  x, P x :=
assume y,
show P y, from sorry
variable U : Type
variable P : U  Prop

example :  y, P y :=
assume x,
show P x, from sorry

example :  x, P x :=
assume y,
show P y, from sorry

这构成了全称量词的引入规则。它与蕴涵的引入规则非常相似:我们不是使用assume来暂时引入一个假设,而是使用assume来暂时引入一个新对象。y(事实上,assumeassume都是 Lean 中单个内部构造的替代语法,也可以用 表示λ。)

This constitutes the introduction rule for the universal quantifier. It is very similar to the introduction rule for implication: instead of using assume to temporarily introduce an assumption, we use assume to temporarily introduce a new object, y. (In fact, assume and assume are both alternate syntax for a single internal construct in Lean, which can also be denoted by λ.)

类似地,消除规则的实施方式如下:

The elimination rule is, similarly, implemented as follows:

variable U : Type
variable P : U  Prop
variable h :  x, P x
variable a : U

example : P a :=
show P a, from h a
variable U : Type
variable P : U  Prop
variable h :  x, P x
variable a : U

example : P a :=
show P a, from h a

观察符号:是通过将假设“应用”于得到的。再次注意与蕴涵消去规则的相似性。P aha

Observe the notation: P a is obtained by “applying” the hypothesis h to a. Once again, note the similarity to the elimination rule for implication.

以下是使用方法的示例:

Here is an example of how it is used:

variable U : Type
variables A B : U  Prop

example (h1 :  x, A x  B x) (h2 :  x, A x) :  x, B x :=
assume y,
have h3 : A y, from h2 y,
have h4 : A y  B y, from h1 y,
show B y, from h4 h3
variable U : Type
variables A B : U  Prop

example (h1 :  x, A x  B x) (h2 :  x, A x) :  x, B x :=
assume y,
have h3 : A y, from h2 y,
have h4 : A y  B y, from h1 y,
show B y, from h4 h3

以下是相同证明的更短版本,我们避免使用have

Here is an even shorter version of the same proof, where we avoid using have:

example (h1 :  x, A x  B x) (h2 :  x, A x) :  x, B x :=
assume y,
show B y, from h1 y (h2 y)
example (h1 :  x, A x  B x) (h2 :  x, A x) :  x, B x :=
assume y,
show B y, from h1 y (h2 y)

你应该在这里讲解这些步骤。将 应用于h1得到y的证明,然后我们将其应用于,这是 的证明。结果就是我们想要的 的证明。A y B yh2 yA yB y

You should talk through the steps, here. Applying h1 to y yields a proof of A y B y, which we then apply to h2 y, which is a proof of A y. The result is the proof of B y that we are after.

在上一章中,我们考虑了自然演绎中的以下证明:

In the last chapter, we considered the following proof in natural deduction:

以下是在 Lean 中呈现的相同证明:

Here is the same proof rendered in Lean:

variable U : Type
variables A B : U  Prop

example : ( x, A x)  ( x, B x)  ( x, A x  B x) :=
assume hA :  x, A x,
assume hB :  x, B x,
assume y,
have Ay : A y, from hA y,
have By : B y, from hB y,
show A y  B y, from and.intro Ay By
variable U : Type
variables A B : U  Prop

example : ( x, A x)  ( x, B x)  ( x, A x  B x) :=
assume hA :  x, A x,
assume hB :  x, B x,
assume y,
have Ay : A y, from hA y,
have By : B y, from hB y,
show A y  B y, from and.intro Ay By

以下是另一个版本,使用了“匿名”版本have

Here is an alternative version, using the “anonymous” versions of have:

variable U : Type
variables A B : U  Prop

example : ( x, A x)  ( x, B x)  ( x, A x  B x) :=
assume hA :  x, A x,
assume hB :  x, B x,
assume y,
have A y, from hA y,
have B y, from hB y,
show A y  B y, from and.intro A y B y
variable U : Type
variables A B : U  Prop

example : ( x, A x)  ( x, B x)  ( x, A x  B x) :=
assume hA :  x, A x,
assume hB :  x, B x,
assume y,
have A y, from hA y,
have B y, from hB y,
show A y  B y, from and.intro A y B y

下面的练习要求你证明上一章讨论过的理发师悖论。你可以只使用命题推理和我们刚刚讨论过的全称量词规则来证明。

The exercises below ask you to prove the barber paradox, which was discussed in the last chapter. You can do that using only propositional reasoning and the rules for the universal quantifier that we have just discussed.

9.3.使用存在量词

9.3. Using the Existential Quantifier

在 Lean 中,您可以通过书写 来输入存在量词\ex。如果您愿意,可以使用 ascii 等效项 。exists引入规则是exists.intro,需要两个参数:一个术语和一个术语满足所需属性的证明。

In Lean, you can type the existential quantifier, , by writing \ex. If you prefer you can use the ascii equivalent, exists. The introduction rule is exists.intro and requires two arguments: a term, and a proof that term satisfies the required property.

variable U : Type
variable P : U  Prop

example (y : U) (h : P y) :  x, P x :=
exists.intro y h
variable U : Type
variable P : U  Prop

example (y : U) (h : P y) :  x, P x :=
exists.intro y h

存在量词的消去规则由 给出exists.elim。它遵循自然演绎规则的形式:如果我们知道并且试图证明,则只需引入一个新变量 ,并在 成立的假设下证明。∃x, P xQyQP y

The elimination rule for the existential quantifier is given by exists.elim. It follows the form of the natural deduction rule: if we know ∃x, P x and we are trying to prove Q, it suffices to introduce a new variable, y, and prove Q under the assumption that P y holds.

variable U : Type
variable P : U  Prop
variable Q : Prop

example (h1 :  x, P x) (h2 :  x, P x  Q) : Q :=
exists.elim h1
  (assume (y : U) (h : P y),
    have h3 : P y  Q, from h2 y,
    show Q, from h3 h)
variable U : Type
variable P : U  Prop
variable Q : Prop

example (h1 :  x, P x) (h2 :  x, P x  Q) : Q :=
exists.elim h1
  (assume (y : U) (h : P y),
    have h3 : P y  Q, from h2 y,
    show Q, from h3 h)

和往常一样,我们可以省略之后的数据类型y和假设信息,因为 Lean 可以从上下文中推断出它们。删除并用其证明替换,得到一个简短(但几乎不可读)的结论证明。hassumeshowh3h2 y

As usual, we can leave off the information as to the data type of y and the hypothesis h after the assume, since Lean can figure them out from the context. Deleting the show and replacing h3 by its proof, h2 y, yields a short (though virtually unreadable) proof of the conclusion.

example (h1 :  x, P x) (h2 :  x, P x  Q) : Q :=
exists.elim h1 (assume y h, h2 y h)
example (h1 :  x, P x) (h2 :  x, P x  Q) : Q :=
exists.elim h1 (assume y h, h2 y h)

下面的示例同时使用了存在量词的引入规则和消除规则。

The following example uses both the introduction and the elimination rules for the existential quantifier.

variable U : Type
variables A B : U  Prop

example : ( x, A x  B x)   x, A x :=
assume h1 :  x, A x  B x,
exists.elim h1
  (assume y (h2 : A y  B y),
    have h3 : A y, from and.left h2,
    show  x, A x, from exists.intro y h3)
variable U : Type
variables A B : U  Prop

example : ( x, A x  B x)   x, A x :=
assume h1 :  x, A x  B x,
exists.elim h1
  (assume y (h2 : A y  B y),
    have h3 : A y, from and.left h2,
    show  x, A x, from exists.intro y h3)

注意假设中的括号;如果我们省略它们,第一个括号之后的所有内容都将包含在该量词的范围内。从假设中,我们得到满足的,因此特别的。所以足以证明结论。 xyA y B yA yy

Notice the parentheses in the hypothesis; if we left them out, everything after the first x would be included in the scope of that quantifier. From the hypothesis, we obtain a y that satisfies A y B y, and hence A y in particular. So y is enough to witness the conclusion.

有时将证明放在exists.elim括号中会很烦人,就像我们在这里对块所做的那样。为了避免这种情况,我们可以使用编程世界中的一些语法,并使用美元符号代替。在 Lean 中,表达式的含义与 相同,优点是我们不必记住关闭括号。使用这个小工具,我们可以按如下方式编写上述证明:assume ... showf $ tf (t)

It is sometimes annoying to enclose the proof after an exists.elim in parenthesis, as we did here with the assume ... show block. To avoid that, we can use a bit of syntax from the programming world, and use a dollar sign instead. In Lean, an expression f $ t means the same thing as f (t), with the advantage that we do not have to remember to close the parenthesis. With this gadget, we can write the proof above as follows:

variable U : Type
variables A B : U  Prop

example : ( x, A x  B x)   x, A x :=
assume h1 :  x, A x  B x,
exists.elim h1 $
assume y (h2 : A y  B y),
have h3 : A y, from and.left h2,
show  x, A x, from exists.intro y h3
variable U : Type
variables A B : U  Prop

example : ( x, A x  B x)   x, A x :=
assume h1 :  x, A x  B x,
exists.elim h1 $
assume y (h2 : A y  B y),
have h3 : A y, from and.left h2,
show  x, A x, from exists.intro y h3

下面的例子更加复杂:

The following example is more involved:

example : ( x, A x  B x)  ( x, A x)  ( x, B x) :=
assume h1 :  x, A x  B x,
exists.elim h1 $
assume y (h2 : A y  B y),
or.elim h2
  (assume h3 : A y,
    have h4 :  x, A x, from exists.intro y h3,
    show ( x, A x)  ( x, B x), from or.inl h4)
  (assume h3 : B y,
    have h4 :  x, B x, from exists.intro y h3,
    show ( x, A x)  ( x, B x), from or.inr h4)
example : ( x, A x  B x)  ( x, A x)  ( x, B x) :=
assume h1 :  x, A x  B x,
exists.elim h1 $
assume y (h2 : A y  B y),
or.elim h2
  (assume h3 : A y,
    have h4 :  x, A x, from exists.intro y h3,
    show ( x, A x)  ( x, B x), from or.inl h4)
  (assume h3 : B y,
    have h4 :  x, B x, from exists.intro y h3,
    show ( x, A x)  ( x, B x), from or.inr h4)

再次注意语句中括号的位置。

Note again the placement of parentheses in the statement.

在上一章中,我们考虑了如下自然演绎证明:

In the last chapter, we considered the following natural deduction proof:

以下是精益中相同含义的证明:

Here is a proof of the same implication in Lean:

variable U : Type
variables A B : U  Prop

example : ( x, A x  ¬ B x)  ¬  x, A x  B x :=
assume h1 :  x, A x  ¬ B x,
assume h2 :  x, A x  B x,
exists.elim h2 $
assume x (h3 : A x  B x),
have h4 : A x, from and.left h3,
have h5 : B x, from and.right h3,
have h6 : ¬ B x, from h1 x h4,
show false, from h6 h5
variable U : Type
variables A B : U  Prop

example : ( x, A x  ¬ B x)  ¬  x, A x  B x :=
assume h1 :  x, A x  ¬ B x,
assume h2 :  x, A x  B x,
exists.elim h2 $
assume x (h3 : A x  B x),
have h4 : A x, from and.left h3,
have h5 : B x, from and.right h3,
have h6 : ¬ B x, from h1 x h4,
show false, from h6 h5

这里,我们使用exists.elim来引入一个x满足 的值。名称是任意的;我们也可以使用:A x B xz

Here, we use exists.elim to introduce a value x satisfying A x B x. The name is arbitrary; we could just as well have used z:

example : ( x, A x  ¬ B x)  ¬  x, A x  B x :=
assume h1 :  x, A x  ¬ B x,
assume h2 :  x, A x  B x,
exists.elim h2 $
assume z (h3 : A z  B z),
have h4 : A z, from and.left h3,
have h5 : B z, from and.right h3,
have h6 : ¬ B z, from h1 z h4,
show false, from h6 h5
example : ( x, A x  ¬ B x)  ¬  x, A x  B x :=
assume h1 :  x, A x  ¬ B x,
assume h2 :  x, A x  B x,
exists.elim h2 $
assume z (h3 : A z  B z),
have h4 : A z, from and.left h3,
have h5 : B z, from and.right h3,
have h6 : ¬ B z, from h1 z h4,
show false, from h6 h5

以下是存在消除规则的另一个示例:

Here is another example of the exists-elimination rule:

variable U : Type
variable u : U
variable P : Prop

example : (x : U, P)  P :=
iff.intro
  (assume h1 : x, P,
    exists.elim h1 $
    assume x (h2 : P),
    h2)
  (assume h1 : P,
    exists.intro u h1)
variable U : Type
variable u : U
variable P : Prop

example : (x : U, P)  P :=
iff.intro
  (assume h1 : x, P,
    exists.elim h1 $
    assume x (h2 : P),
    h2)
  (assume h1 : P,
    exists.intro u h1)

u这很微妙:如果我们不声明类型的变量U,即使没有出现在定理的陈述中,证明也不会通过u。这突出了一阶逻辑和 Lean 中实现的逻辑之间的区别。在自然演绎中,我们可以证明xP(x)xP(x),这表明我们的证明系统隐含地假设宇宙至少有一个对象。相反,该陈述在 Lean 中是不可证明的。换句话说,在 Lean 中,类型可能为空,因此上述证明需要明确假设中有一个元素。(∀ x : U, P x) x : U, P xuU

This is subtle: the proof does not go through if we do not declare a variable u of type U, even though u does not appear in the statement of the theorem. This highlights a difference between first-order logic and the logic implemented in Lean. In natural deduction, we can prove xP(x)xP(x), which shows that our proof system implicitly assumes that the universe has at least one object. In contrast, the statement (∀ x : U, P x) x : U, P x is not provable in Lean. In other words, in Lean, it is possible for a type to be empty, and so the proof above requires an explicit assumption that there is an element u in U.

9.4.等式和计算证明

9.4. Equality and calculational proofs

在精益中,反身性、对称性和传递性分别称为eq.refleq.symmeq.trans,而第二条替换规则称为eq.subst。它们的用途如下所示。

In Lean, reflexivity, symmetry, and transitivity are called eq.refl, eq.symm, and eq.trans, and the second substitution rule is called eq.subst. Their uses are illustrated below.

variable A : Type

variables x y z : A
variable P : A  Prop

example : x = x :=
show x = x, from eq.refl x

example : y = x :=
have h : x = y, from sorry,
show y = x, from eq.symm h

example : x = z :=
have h1 : x = y, from sorry,
have h2 : y = z, from sorry,
show x = z, from eq.trans h1 h2

example : P y :=
have h1 : x = y, from sorry,
have h2 : P x, from sorry,
show P y, from eq.subst h1 h2
variable A : Type

variables x y z : A
variable P : A  Prop

example : x = x :=
show x = x, from eq.refl x

example : y = x :=
have h : x = y, from sorry,
show y = x, from eq.symm h

example : x = z :=
have h1 : x = y, from sorry,
have h2 : y = z, from sorry,
show x = z, from eq.trans h1 h2

example : P y :=
have h1 : x = y, from sorry,
have h2 : P x, from sorry,
show P y, from eq.subst h1 h2

上述规则eq.refl假设x为论据,因为没有假设可以推断它。所有其他规则都假设其前提为论据。以下是等式推理的一个例子:

The rule eq.refl above assumes x as an argument, because there is no hypothesis to infer it from. All the other rules assume their premises as arguments. Here is an example of equational reasoning:

variables (A : Type) (x y z : A)

example : y = x  y = z  x = z :=
assume h1 : y = x,
assume h2 : y = z,
have h3 : x = y, from eq.symm h1,
show x = z, from eq.trans h3 h2
variables (A : Type) (x y z : A)

example : y = x  y = z  x = z :=
assume h1 : y = x,
assume h2 : y = z,
have h3 : x = y, from eq.symm h1,
show x = z, from eq.trans h3 h2

这个证明可以写得更简洁一些:

This proof can be written more concisely:

example : y = x  y = z  x = z :=
assume h1 h2, eq.trans (eq.symm h1) h2
example : y = x  y = z  x = z :=
assume h1 h2, eq.trans (eq.symm h1) h2

然而,因为计算在数学中非常重要,所以 Lean 提供了更有效的方法来实现计算。一种方法是使用策略。在任何需要证明的地方rewrite键入begin和都会将 Lean 置于策略模式,这提供了一种编写证明的另一种方法:不是直接编写证明,而是向 Lean 提供一系列指令,这些指令向 Lean 展示如何构建相关陈述的证明。要证明的陈述称为目标许多指令通过将目标转换为更容易证明的内容来取得进展。对目标进行替换的命令就是一个很好的例子。前面的例子可以按如下方式证明:endrewrite

Because calculations are so important in mathematics, however, Lean provides more efficient ways of carrying them out. One method is to use the rewrite tactic. Typing begin and end anywhere a proof is expected puts Lean into tactic mode, which provides an alternative way of writing a proof: rather than writing it directly, you provide Lean with a list of instructions that show Lean how to construct a proof of the statement in question. The statement to be proved is called the goal, and many instructions make progress by transforming the goal into something that is easier to prove. The rewrite command, which carries out a substitution on the goal, is a good example. The previous example can be proved as follows:

example : y = x  y = z  x = z :=
assume h1 : y = x,
assume h2 : y = z,
show x = z,
  begin
    rewrite h1,
    apply h2
  end
example : y = x  y = z  x = z :=
assume h1 : y = x,
assume h2 : y = z,
show x = z,
  begin
    rewrite h1,
    apply h2
  end

如果将光标放在单词 之后begin,Lean 会告诉您此时的目标是证明。第一个命令将目标更改为;之前的左箭头(您可以输入)告诉 Lean 反向使用方程式。如果将光标放在逗号之后,Lean 会向您显示新目标 。该命令用于完成证明。x = zx = zy = zh1\<-y = zapplyh2

If you put the cursor after the word begin, Lean will tell you that the goal at that point is to prove x = z. The first command changes the goal x = z to y = z; the left-facing arrow before h1 (which you can enter as \<-) tells Lean to use the equation in the reverse direction. If you put the cursor after the comma, Lean shows you the new goal, y = z. The apply command uses h2 to complete the proof.

h1另一种方法是使用和重写目标h2,从而将目标简化为。发生这种情况时,会自动应用反身性。重写是精益中非常常见的操作,因此我们可以使用简写代替完整的。x = xrewriterwrewrite

An alternative is to rewrite the goal using h1 and h2, which reduces the goal to x = x. When that happens, rewrite automatically applies reflexivity. Rewriting is such a common operation in Lean that we can use the shorthand rw in place of the full rewrite.

example : y = x  y = z  x = z :=
assume h1 : y = x,
assume h2 : y = z,
show x = z,
  begin
    rw h1,
    rw h2
  end
example : y = x  y = z  x = z :=
assume h1 : y = x,
assume h2 : y = z,
show x = z,
  begin
    rw h1,
    rw h2
  end

事实上,可以使用方括号组合一系列重写:

In fact, a sequence of rewrites can be combined, using square brackets:

example : y = x  y = z  x = z :=
assume h1 : y = x,
assume h2 : y = z,
show x = z,
  begin
    rw [h1, h2]
  end
example : y = x  y = z  x = z :=
assume h1 : y = x,
assume h2 : y = z,
show x = z,
  begin
    rw [h1, h2]
  end

当您将证明简化为单一策略时,您可以使用by而不是。begin ... end

And when you reduce a proof to a single tactic, you can use by instead of begin ... end.

example : y = x  y = z  x = z :=
assume h1 : y = x,
assume h2 : y = z,
show x = z, by rw [h1, h2]
example : y = x  y = z  x = z :=
assume h1 : y = x,
assume h2 : y = z,
show x = z, by rw [h1, h2]

如果您将光标放在 之后←h1,Lean 会显示该点的目标。

If you put the cursor after the ←h1, Lean shows you the goal at that point.

在接下来的章节中我们将看到,在普通的数学证明中,人们通常以这样的格式进行计算:

We will see in the coming chapters that in ordinary mathematical proofs, one commonly carries out calculations in a format like this:

t1=t2=t3=t4=t5.

Lean 有一种机制来模拟此类计算证明。每当需要证明某个等式时,您都可以使用标识符 提供证明calc,然后跟上一系列等式和证明,形式如下:

Lean has a mechanism to model such calculational proofs. Whenever a proof of an equation is expected, you can provide a proof using the identifier calc, following by a chain of equalities and justification, in the following form:

calc
  e1 = e2    : justification 1
    ... = e3 : justification 2
    ... = e4 : justification 3
    ... = e5 : justification 4
calc
  e1 = e2    : justification 1
    ... = e3 : justification 2
    ... = e4 : justification 3
    ... = e5 : justification 4

这个链条可以一直延伸到需要的长度,在这个例子中,结果是 的证明。每个证明都是所用假设或定理的名称。例如,前面的证明可以写成如下形式:e1 = e5

The chain can go on as long as needed, and in this example the result is a proof of e1 = e5. Each justification is the name of the assumption or theorem that is used. For example, the previous proof could be written as follows:

example : y = x  y = z  x = z :=
assume h1 : y = x,
assume h2 : y = z,
calc
    x = y : eq.symm h1
  ... = z : h2
example : y = x  y = z  x = z :=
assume h1 : y = x,
assume h2 : y = z,
calc
    x = y : eq.symm h1
  ... = z : h2

像往常一样,语法很挑剔;请注意,表达式中没有逗号calc,冒号和点需要以该形式准确输入。唯一不同的是表达式和​​理由本身。e1, e2, e3, ...

As usual, the syntax is finicky; notice that there are no commas in the calc expression, and the colons and dots need to be entered exactly in that form. All that varies are the expressions e1, e2, e3, ... and the justifications themselves.

环境calc与 结合使用时功能最强大rewrite,因为我们可以使用库中的事实重写表达式。例如,Lean 的库有许多整数的基本恒等式,例如:

The calc environment is most powerful when used in conjunction with rewrite, since we can then rewrite expressions with facts from the library. For example, Lean’s library has a number of basic identities for the integers, such as these:

import data.int.basic

variables x y z : int

example : x + 0 = x :=
add_zero x

example : 0 + x = x :=
zero_add x

example : (x + y) + z = x + (y + z) :=
add_assoc x y z

example : x + y = y + x :=
add_comm x y

example : (x * y) * z = x * (y * z) :=
mul_assoc x y z

example : x * y = y * x :=
mul_comm x y

example : x * (y + z) = x * y + x * z :=
left_distrib x y z

example : (x + y) * z = x * z + y * z :=
right_distrib x y z
import data.int.basic

variables x y z : int

example : x + 0 = x :=
add_zero x

example : 0 + x = x :=
zero_add x

example : (x + y) + z = x + (y + z) :=
add_assoc x y z

example : x + y = y + x :=
add_comm x y

example : (x * y) * z = x * (y * z) :=
mul_assoc x y z

example : x * y = y * x :=
mul_comm x y

example : x * (y + z) = x * y + x * z :=
left_distrib x y z

example : (x + y) * z = x * z + y * z :=
right_distrib x y z

您还可以将整数类型写为,使用\Z或输入\int我们已经导入了文件data.int.basic,以便我们可以使用整数的所有基本属性。 (在后面的代码片段中,我们将在在线和 pdf 版本的教科书中删除此行,以避免混乱。)例如,请注意add_comm是定理。因此,要将其实例化为,您可以写。使用这些公理,以下是在 Lean 中呈现的上述计算,作为关于整数的定理: x y, x + y = y + xs + t = t + sadd_comm s t

You can also write the type of integers as , entered with either \Z or \int We have imported the file data.int.basic to make all the basic properties of the integers available to us. (In later snippets, we will suppress this line in the online and pdf versions of the textbook, to avoid clutter.) Notice that, for example, add_comm is the theorem x y, x + y = y + x. So to instantiate it to s + t = t + s, you write add_comm s t. Using these axioms, here is the calculation above rendered in Lean, as a theorem about the integers:

example (x y z : int) : (x + y) + z = (x + z) + y :=
calc
   (x + y) + z = x + (y + z) : add_assoc x y z
           ... = x + (z + y) : eq.subst (add_comm y z) rfl
           ... = (x + z) + y : eq.symm (add_assoc x z y)
example (x y z : int) : (x + y) + z = (x + z) + y :=
calc
   (x + y) + z = x + (y + z) : add_assoc x y z
           ... = x + (z + y) : eq.subst (add_comm y z) rfl
           ... = (x + z) + y : eq.symm (add_assoc x z y)

使用rewrite更有效,但有时我们必须提供信息来指定规则的使用位置:

Using rewrite is more efficient, though at times we have to provide information to specify where the rules are used:

example (x y z : int) : (x + y) + z = (x + z) + y :=
calc
  (x + y) + z = x + (y + z) : by rw add_assoc
          ... = x + (z + y) : by rw [add_comm y z]
          ... = (x + z) + y : by rw add_assoc
example (x y z : int) : (x + y) + z = (x + z) + y :=
calc
  (x + y) + z = x + (y + z) : by rw add_assoc
          ... = x + (z + y) : by rw [add_comm y z]
          ... = (x + z) + y : by rw add_assoc

在这种情况下,我们可以使用一个rewrite

In that case, we can use a single rewrite:

example (x y z : int) : (x + y) + z = (x + z) + y :=
by rw [add_assoc, add_comm y z, add_assoc]
example (x y z : int) : (x + y) + z = (x + z) + y :=
by rw [add_assoc, add_comm y z, add_assoc]

如果在序列rewrites充分之前#check证明,则错误消息将显示剩余的目标。

If you #check the proof before the sequence of rewrites is sufficient, the error message will display the remaining goal.

下面是另一个示例:

Here is another example:

variables a b d c : int

example : (a + b) * (c + d) = a * c + b * c + a * d + b * d :=
calc
  (a + b) * (c + d) = (a + b) * c + (a + b) * d : by rw left_distrib
    ... = (a * c + b * c) + (a + b) * d         : by rw right_distrib
    ... = (a * c + b * c) + (a * d + b * d)     : by rw right_distrib
    ... = a * c + b * c + a * d + b * d         : by rw add_assoc
variables a b d c : int

example : (a + b) * (c + d) = a * c + b * c + a * d + b * d :=
calc
  (a + b) * (c + d) = (a + b) * c + (a + b) * d : by rw left_distrib
    ... = (a * c + b * c) + (a + b) * d         : by rw right_distrib
    ... = (a * c + b * c) + (a * d + b * d)     : by rw right_distrib
    ... = a * c + b * c + a * d + b * d         : by rw add_assoc

再次,我们可以得到一个更短的证明:

Once again, we can get by with a shorter proof:

example : (a + b) * (c + d) = a * c + b * c + a * d + b * d :=
by rw [left_distrib, right_distrib, right_distrib, add_assoc]
example : (a + b) * (c + d) = a * c + b * c + a * d + b * d :=
by rw [left_distrib, right_distrib, right_distrib, add_assoc]

9.5.练习

9.5. Exercises

  1. 填写sorry

    section
      variable A : Type
      variable f : A  A
      variable P : A  Prop
      variable  h :  x, P x  P (f x)
    
      -- Show the following:
      example :  y, P y  P (f (f y)) :=
      sorry
    end
    
  2. Fill in the sorry.

    section
      variable A : Type
      variable f : A  A
      variable P : A  Prop
      variable  h :  x, P x  P (f x)
    
      -- Show the following:
      example :  y, P y  P (f (f y)) :=
      sorry
    end
    
  3. 填写sorry

    section
      variable U : Type
      variables A B : U  Prop
    
      example : ( x, A x  B x)   x, A x :=
      sorry
    end
    
  4. Fill in the sorry.

    section
      variable U : Type
      variables A B : U  Prop
    
      example : ( x, A x  B x)   x, A x :=
      sorry
    end
    
  5. 填写sorry

    section
      variable U : Type
      variables A B C : U  Prop
    
      variable h1 :  x, A x  B x
      variable h2 :  x, A x  C x
      variable h3 :  x, B x  C x
    
      example :  x, C x :=
      sorry
    end
    
  6. Fill in the sorry.

    section
      variable U : Type
      variables A B C : U  Prop
    
      variable h1 :  x, A x  B x
      variable h2 :  x, A x  C x
      variable h3 :  x, B x  C x
    
      example :  x, C x :=
      sorry
    end
    
  7. 填写sorry下面的,以证明理发师悖论。

    open classical   -- not needed, but you can use it
    
    -- This is an exercise from Chapter 4. Use it as an axiom here.
    axiom not_iff_not_self (P : Prop) : ¬ (P  ¬ P)
    
    example (Q : Prop) : ¬ (Q  ¬ Q) :=
    not_iff_not_self Q
    
    section
      variable Person : Type
      variable shaves : Person  Person  Prop
      variable barber : Person
      variable h :  x, shaves barber x  ¬ shaves x x
    
      -- Show the following:
      example : false :=
      sorry
    end
    
  8. Fill in the sorry’s below, to prove the barber paradox.

    open classical   -- not needed, but you can use it
    
    -- This is an exercise from Chapter 4. Use it as an axiom here.
    axiom not_iff_not_self (P : Prop) : ¬ (P  ¬ P)
    
    example (Q : Prop) : ¬ (Q  ¬ Q) :=
    not_iff_not_self Q
    
    section
      variable Person : Type
      variable shaves : Person  Person  Prop
      variable barber : Person
      variable h :  x, shaves barber x  ¬ shaves x x
    
      -- Show the following:
      example : false :=
      sorry
    end
    
  9. 填写sorry

    section
      variable U : Type
      variables A B : U  Prop
    
      example : ( x, A x)   x, A x  B x :=
      sorry
    end
    
  10. Fill in the sorry.

    section
      variable U : Type
      variables A B : U  Prop
    
      example : ( x, A x)   x, A x  B x :=
      sorry
    end
    
  11. 填写sorry

    section
      variable U : Type
      variables A B : U  Prop
    
      variable h1 :  x, A x  B x
      variable h2 :  x, A x
    
      example :  x, B x :=
      sorry
    end
    
  12. Fill in the sorry.

    section
      variable U : Type
      variables A B : U  Prop
    
      variable h1 :  x, A x  B x
      variable h2 :  x, A x
    
      example :  x, B x :=
      sorry
    end
    
  13. 填写sorry

    variable  U : Type
    variables A B C : U  Prop
    
    example (h1 :  x, A x  B x) (h2 :  x, B x  C x) :
         x, A x  C x :=
    sorry
    
  14. Fill in the sorry.

    variable  U : Type
    variables A B C : U  Prop
    
    example (h1 :  x, A x  B x) (h2 :  x, B x  C x) :
         x, A x  C x :=
    sorry
    
  15. 完成这些证明。

    variable  U : Type
    variables A B C : U  Prop
    
    example : (¬  x, A x)   x, ¬ A x :=
    sorry
    
    example : ( x, ¬ A x)  ¬  x, A x :=
    sorry
    
  16. Complete these proofs.

    variable  U : Type
    variables A B C : U  Prop
    
    example : (¬  x, A x)   x, ¬ A x :=
    sorry
    
    example : ( x, ¬ A x)  ¬  x, A x :=
    sorry
    
  17. 填写sorry

    variable  U : Type
    variables R : U  U  Prop
    
    example : ( x,  y, R x y)   y,  x, R x y :=
    sorry
    
  18. Fill in the sorry.

    variable  U : Type
    variables R : U  U  Prop
    
    example : ( x,  y, R x y)   y,  x, R x y :=
    sorry
    
  19. 下面的练习表明,在存在反身性的情况下,对称性和传递性规则等同于单一规则。

    theorem foo {A : Type} {a b c : A} : a = b  c = b  a = c :=
    sorry
    
    -- notice that you can now use foo as a rule. The curly braces mean that
    -- you do not have to give A, a, b, or c
    
    section
      variable A : Type
      variables a b c : A
    
      example (h1 : a = b) (h2 : c = b) : a = c :=
      foo h1 h2
    end
    
    section
      variable {A : Type}
      variables {a b c : A}
    
      -- replace the sorry with a proof, using foo and rfl, without using eq.symm.
      theorem my_symm (h : b = a) : a = b :=
      sorry
    
      -- now use foo and my_symm to prove transitivity
      theorem my_trans (h1 : a = b) (h2 : b = c) : a = c :=
      sorry
    end
    
  20. The following exercise shows that in the presence of reflexivity, the rules for symmetry and transitivity are equivalent to a single rule.

    theorem foo {A : Type} {a b c : A} : a = b  c = b  a = c :=
    sorry
    
    -- notice that you can now use foo as a rule. The curly braces mean that
    -- you do not have to give A, a, b, or c
    
    section
      variable A : Type
      variables a b c : A
    
      example (h1 : a = b) (h2 : c = b) : a = c :=
      foo h1 h2
    end
    
    section
      variable {A : Type}
      variables {a b c : A}
    
      -- replace the sorry with a proof, using foo and rfl, without using eq.symm.
      theorem my_symm (h : b = a) : a = b :=
      sorry
    
      -- now use foo and my_symm to prove transitivity
      theorem my_trans (h1 : a = b) (h2 : b = c) : a = c :=
      sorry
    end
    
  21. sorry用列表中的正确公理替换下面的每一个。

    import data.int.basic
    
    -- these are the axioms for a commutative ring
    
    #check @add_assoc
    #check @add_comm
    #check @add_zero
    #check @zero_add
    #check @mul_assoc
    #check @mul_comm
    #check @mul_one
    #check @one_mul
    #check @left_distrib
    #check @right_distrib
    #check @add_left_neg
    #check @add_right_neg
    #check @sub_eq_add_neg
    
    variables x y z : int
    
    theorem t1 : x - x = 0 :=
    calc
    x - x = x + -x : by rw sub_eq_add_neg
        ... = 0      : by rw add_right_neg
    
    theorem t2 (h : x + y = x + z) : y = z :=
    calc
    y     = 0 + y        : by rw zero_add
        ... = (-x + x) + y : by rw add_left_neg
        ... = -x + (x + y) : by rw add_assoc
        ... = -x + (x + z) : by rw h
        ... = (-x + x) + z : by rw add_assoc
        ... = 0 + z        : by rw add_left_neg
        ... = z            : by rw zero_add
    
    theorem t3 (h : x + y = z + y) : x = z :=
    calc
    x     = x + 0        : sorry
        ... = x + (y + -y) : sorry
        ... = (x + y) + -y : sorry
        ... = (z + y) + -y : by rw h
        ... = z + (y + -y) : sorry
        ... = z + 0        : sorry
        ... = z            : sorry
    
    theorem t4 (h : x + y = 0) : x = -y :=
    calc
    x     = x + 0        : by rw add_zero
        ... = x + (y + -y) : by rw add_right_neg
        ... = (x + y) + -y : by rw add_assoc
        ... = 0 + -y       : by rw h
        ... = -y           : by rw zero_add
    
    theorem t5 : x * 0 = 0 :=
    have h1 : x * 0 + x * 0 = x * 0 + 0, from
    calc
        x * 0 + x * 0 = x * (0 + 0) : sorry
                ... = x * 0       : sorry
                ... = x * 0 + 0   : sorry,
    show x * 0 = 0, from t2 _ _ _ h1
    
    theorem t6 : x * (-y) = -(x * y) :=
    have h1 : x * (-y) + x * y = 0, from
    calc
        x * (-y) + x * y = x * (-y + y) : sorry
                    ... = x * 0        : sorry
                    ... = 0            : by rw t5 x,
    show x * (-y) = -(x * y), from t4 _ _ h1
    
    theorem t7 : x + x = 2 * x :=
    calc
    x + x = 1 * x + 1 * x : by rw one_mul
        ... = (1 + 1) * x   : sorry
        ... = 2 * x         : rfl
    
  22. Replace each sorry below by the correct axiom from the list.

    import data.int.basic
    
    -- these are the axioms for a commutative ring
    
    #check @add_assoc
    #check @add_comm
    #check @add_zero
    #check @zero_add
    #check @mul_assoc
    #check @mul_comm
    #check @mul_one
    #check @one_mul
    #check @left_distrib
    #check @right_distrib
    #check @add_left_neg
    #check @add_right_neg
    #check @sub_eq_add_neg
    
    variables x y z : int
    
    theorem t1 : x - x = 0 :=
    calc
    x - x = x + -x : by rw sub_eq_add_neg
        ... = 0      : by rw add_right_neg
    
    theorem t2 (h : x + y = x + z) : y = z :=
    calc
    y     = 0 + y        : by rw zero_add
        ... = (-x + x) + y : by rw add_left_neg
        ... = -x + (x + y) : by rw add_assoc
        ... = -x + (x + z) : by rw h
        ... = (-x + x) + z : by rw add_assoc
        ... = 0 + z        : by rw add_left_neg
        ... = z            : by rw zero_add
    
    theorem t3 (h : x + y = z + y) : x = z :=
    calc
    x     = x + 0        : sorry
        ... = x + (y + -y) : sorry
        ... = (x + y) + -y : sorry
        ... = (z + y) + -y : by rw h
        ... = z + (y + -y) : sorry
        ... = z + 0        : sorry
        ... = z            : sorry
    
    theorem t4 (h : x + y = 0) : x = -y :=
    calc
    x     = x + 0        : by rw add_zero
        ... = x + (y + -y) : by rw add_right_neg
        ... = (x + y) + -y : by rw add_assoc
        ... = 0 + -y       : by rw h
        ... = -y           : by rw zero_add
    
    theorem t5 : x * 0 = 0 :=
    have h1 : x * 0 + x * 0 = x * 0 + 0, from
    calc
        x * 0 + x * 0 = x * (0 + 0) : sorry
                ... = x * 0       : sorry
                ... = x * 0 + 0   : sorry,
    show x * 0 = 0, from t2 _ _ _ h1
    
    theorem t6 : x * (-y) = -(x * y) :=
    have h1 : x * (-y) + x * y = 0, from
    calc
        x * (-y) + x * y = x * (-y + y) : sorry
                    ... = x * 0        : sorry
                    ... = 0            : by rw t5 x,
    show x * (-y) = -(x * y), from t4 _ _ h1
    
    theorem t7 : x + x = 2 * x :=
    calc
    x + x = 1 * x + 1 * x : by rw one_mul
        ... = (1 + 1) * x   : sorry
        ... = 2 * x         : rfl
    

10.一阶逻辑的语义

10. Semantics of First Order Logic

在第 6 章中,我们强调了命题逻辑的语法语义之间的区别。语法问题与公式的形式结构以及可以得出不同类型公式的条件有关。另一方面,语义问题涉及公式相对于某些真值赋值的真值

In Chapter 6, we emphasized a distinction between the syntax and the semantics of propositional logic. Syntactic questions have to do with the formal structure of formulas and the conditions under which different types of formulas can be derived. Semantic questions, on the other hand, concern the truth of a formula relative to some truth assignment.

正如你所料,我们可以在一阶逻辑的设置中做出类似的区分。前两章重点介绍了语法,但也引入了一些语义思想。回想一下感兴趣的领域N、常数符号0、1、2、3、函数符号addmul和谓词符号even,prime,equals,le等。我们知道这句话yle(0,y)在这个例子中是真的,如果le被解释为自然数上的小于或等于关系。但是如果我们考虑域Z而不是N,则该公式将变为假。句子ylt(0,y)如果我们考虑域,也是错误的N,但(有些反常地)解释谓词lt(x,y)作为关系“x大于y” 在自然数上。

As you might expect, we can make a similar distinction in the setting of first order logic. The previous two chapters have focused on syntax, but some semantic ideas have slipped in. Recall the running example with domain of interest N, constant symbols 0, 1, 2, 3, function symbols add and mul, and predicate symbols even,prime,equals,le, etc. We know that the sentence yle(0,y) is true in this example, if le is interpreted as the less-than-or-equal-to relation on the natural numbers. But if we consider the domain Z instead of N, that same formula becomes false. The sentence ylt(0,y) is also false if we consider the domain N, but (somewhat perversely) interpret the predicate lt(x,y) as the relation “x is greater than y” on the natural numbers.

这表明,一阶句子的真假取决于我们如何解释量词和语言的基本关系。但有些公式在任何解释下都是正确的:例如,y(le(0,y)le(0,y))在上一段中考虑的所有解释下都是正确的,事实上,在我们选择的任何解释下都是正确的。像这样的句子被称为有效的;这类似于命题逻辑中的同义反复,它在任何可能的真值赋值下都是正确的。

This indicates that the truth or falsity or a first order sentence can depend on how we interpret the quantifiers and basic relations of the language. But some formulas are true under any interpretation: for instance, y(le(0,y)le(0,y)) is true under all the interpretations considered in the last paragraph, and, indeed, under any interpretation we choose. A sentence like this is said to be valid; this is the analogue of a tautology in propositional logic, which is true under every possible truth assignment.

我们可以扩大类比范围:一阶逻辑中的“模型”类似于命题逻辑中的真值分配。在命题情况下,选择真值分配使我们能够为语言的所有公式分配真值;现在,选择模型将使我们能够为一阶语言的所有句子分配真值。下一节的目的是使这个概念更加精确。

We can broaden the analogy: a “model” in first order logic is the analogue of a truth assignment in propositional logic. In the propositional case, choosing a truth assignment allowed us to assign truth values to all formulas of the language; now, choosing a model will allow us to assign truth values to all sentences of a first order language. The aim of the next section is to make this notion more precise.

10.1.解释

10.1. Interpretations

在我们的示例中,语言的符号是 0、1、addprime等等——都有非常具有启发性的名字。当我们在域上解释这种语言的句子时N例如,可以清楚地知道域中的哪些元素prime“应该”为真,而“应该”为假。但让我们考虑一个只有两个一元谓词符号的一阶语言fancytall. 如果我们把我们的领域设为N,是句子x(fancy(x)tall(x))真还是假?

The symbols of the language in our running example—0, 1, add, prime, and so on—have very suggestive names. When we interpret sentences of this language over the domain N, for example, it is clear for which elements of the domain prime “should” be true, and for which it “should” be false. But let us consider a first order language that has only two unary predicate symbols fancy and tall. If we take our domain to be N, is the sentence x(fancy(x)tall(x)) true or false?

答案当然是,我们没有足够的信息来说明。谓词没有明显的含义fancy或者tall,至少当我们将它们应用于自然数时不是。为了理解这句话,我们需要知道哪些数字是奇特的,哪些数字是高的。也许 10 的倍数是奇特的,而偶数是高的;在这种情况下,公式是正确的,因为 10 的每个倍数都是偶数。也许质数是奇特的,而奇数是高的;那么公式就是错误的,因为 2 是奇特的,但不高。

The answer, of course, is that we don’t have enough information to say. There’s no obvious meaning to the predicates fancy or tall, at least not when we apply them to natural numbers. To make sense of the sentence, we need to know which numbers are fancy and which ones are tall. Perhaps multiples of 10 are fancy, and even numbers are tall; in this case, the formula is true, since every multiple of 10 is even. Perhaps prime numbers are fancy and odd numbers are tall; then the formula is false, since 2 is fancy but not tall.

我们将这些描述称为谓词符号的解释fancytall在域中N. 正式来说,一元谓词的解释P在域中D是元素的集合D为此P是正确的。例如,primeN我们上面使用的只是质自然数的集合。

We call each of these descriptions an interpretation of the predicate symbols fancy and tall in the domain N. Formally, an interpretation of a unary predicate P in a domain D is the set of elements of D for which P is true. For an example, the standard interpretation of prime in N that we used above was just the set of prime natural numbers.

我们可以以类似的方式解释常数、函数和关系符号。常数符号的解释c在域中D是...的一个元素D. 函数符号的解释f具有元数n是一个映射函数n元素D另一个元素D. 关系符号的解释R具有元数nn元素元组D为此R是真的。

We can interpret constant, function, and relation symbols in a similar way. An interpretation of constant symbol c in domain D is an element of D. An interpretation of a function symbol f with arity n is a function that maps n elements of D to another element of D. An interpretation of a relation symbol R with arity n is the set of n tuples of elements of D for which R is true.

强调句法谓词符号(或函数符号、常量符号)与它所解释的语义谓词(或函数、对象)之间的区别非常重要。前者是一个符号,与其他符号相关,在我们指定解释之前,它本身没有任何意义。严格来说,写成prime(3), 在哪里prime是谓词符号,3 是自然数,因为prime应该是一个句法术语。有时我们可能会混淆这种区别,就像上面我们用常数符号 0、1 和 2 指定语言时一样。但是,域的对象和我们用来表示它们的符号之间仍然存在根本区别。

It is important to emphasize the difference between a syntactic predicate symbol (or function symbol, or constant symbol) and the semantic predicate (or function, or object) to which it is interpreted. The former is a symbol, relates to other symbols, and has no meaning on its own until we specify an interpretation. Strictly speaking, it makes no sense to write prime(3), where prime is a predicate symbol and 3 is a natural number, since the argument to prime is supposed to be a syntactic term. Sometimes we may obscure this distinction, as above when we specified a language with constant symbols 0, 1, and 2. But there is still a fundamental distinction between the objects of the domain and the symbols we use to represent them.

有时,当我们在特定领域解释一种语言时,隐式地在语言中引入新的常量符号来表示该领域的元素是很有用的。具体来说,对于每个元素a在域中,我们引入一个常数符号a¯,解释为a.然后表达式prime(3¯)确实有意义。解释谓词符号prime自然地,这个表达式的计算结果为真。我们认为3¯作为代表自然数 3 的语言“名称”,就像“亚里士多德”这个短语是代表古希腊哲学家的名字一样。

Sometimes, when we interpret a language in a particular domain, it is useful to implicitly introduce new constant symbols into the language to denote elements of this domain. Specifically, for each element a of the domain, we introduce a constant symbol a¯, which is interpreted as a. Then the expression prime(3¯) does make sense. Interpreting the predicate symbol prime in the natural way, this expression will evaluate to true. We think of 3¯ as a linguistic “name” that represents the natural number 3, in the same way that the phrase “Aristotle” is a name that represents the ancient Greek philosopher.

10.2.模型中的真相

10.2. Truth in a Model

修复一阶语言。假设我们选择了一个域D在其中解释语言,以及D该语言的每个符号。我们将这种结构称为域D,与解释配对——语言模型。一阶语言模型与命题逻辑的真值分配直接类似,因为它提供了我们确定语言中每个句子的真值所需的所有信息。

Fix a first-order language. Suppose we have chosen a domain D in which to interpret the language, along with an interpretation in D of each of the symbols of that language. We will call this structure—the domain D, paired with the interpretation—a model for the language. A model for a first-order language is directly analogous to a truth assignment for propositional logic, because it provides all the information we need to determine the truth value of each sentence in the language.

基于模型评估句子真实性的过程正如你所想的那样,但正式的描述却很微妙。回想一下我们在第 4 章中提到的术语断言之间的区别。术语,如ax+y, 或者f(c),旨在表示对象。术语没有真值,因为(例如)询问 3 是真还是假是没有意义的。断言,如P(a)R(x,f(y)), 或者a+b>aprime(c),将谓词或关系符号应用于术语以产生可能为真或为假的陈述。

The procedure for evaluating the truth of a sentence based on a model works the way you think it should, but the formal description is subtle. Recall the difference between terms and assertions that we made earlier in Chapter 4. Terms, like a, x+y, or f(c), are meant to represent objects. A term does not have a truth value, since (for example) it makes no sense to ask whether 3 is true or false. Assertions, like P(a), R(x,f(y)), or a+b>aprime(c), apply predicate or relation symbols to terms to produce statements that could be true or false.

模型中术语的解释是该模型域的一个元素。模型直接指定如何解释常量符号。解释术语f(t)通过将函数符号应用到另一个项上而创建的,我们解释为t,然后应用解释f(这个过程是有意义的,因为对这个词的解释f是域上的函数。)这显然可以推广到更高元数的函数。我们暂时不会解释包含自由变量的术语,例如xy,因为这些项没有选出域中独特的元素。(变量x可能可以指代任何对象。)

The interpretation of a term in a model is an element of the domain of that model. The model directly specifies how to interpret constant symbols. To interpret a term f(t) created by applying a function symbol to another term, we interpret the term t, and then apply the interpretation of f to this term. (This process makes sense, since the interpretation of f is a function on the domain.) This generalizes to functions of higher arity in the obvious way. We will not yet interpret terms that include free variables like x and y, since these terms do not pick out unique elements of the domain. (The variable x could potentially refer to any object.)

例如,假设我们有一种具有两个常量符号的语言,ab,一元函数符号f以及二元函数符号g。 让M是具有域的模型N, 在哪里ab被解释为35, 分别,f(x)被解释为映射任何自然数的函数nn2, 和g是加法函数。那么g(f(a),b)表示自然数32+5=14

For example, suppose we have a language with two constant symbols, a and b, a unary function symbol f, and a binary function symbol g. Let M be the model with domain N, where a and b are interpreted as 3 and 5, respectively, f(x) is interpreted as the function which maps any natural number n to n2, and g is the addition function. Then the term g(f(a),b) denotes the natural number 32+5=14.

类似地,断言的解释是一个值T或者F。为了简洁起见,我们在这里引入新的符号:如果A是一个断言,M是语言的典范A,我们写MA意思是A评估结果为TM, 和MA意思是A评估结果为F. (你可以阅读符号作为“模型”或“满足”或“验证”。)

Similarly, the interpretation of an assertion is a value T or F. For the sake of brevity, we will introduce new notation here: if A is an assertion and M is a model of the language of A, we write MA to mean that A evaluates to T in M, and MA to mean that A evaluates to F. (You can read the symbol as “models” or “satisfies” or “validates.”)

要解释应用于某些术语的谓词或关系,我们首先将这些术语解释为域中的对象,然后查看关系符号的解释是否适用于这些对象。继续这个例子,假设我们的语言也有一个关系符号R,我们扩展M解释R作为大于或等于关系。然后我们有MR(a,b),因为 3 不大于 5,但是MR(g(f(a),b),b),因为 14 大于 5。

To interpret a predicate or relation applied to some terms, we first interpret the terms as objects in the domain, and then see if the interpretation of the relation symbol is true of those objects. To continue with the example, suppose our language also has a relation symbol R, and we extend M to interpret R as the greater-than-or-equal-to relation. Then we have MR(a,b), since 3 is not greater than 5, but MR(g(f(a),b),b), since 14 is greater than 5.

使用逻辑连接词解释表达式, 和¬其工作原理与命题设置中完全相同。MAB确切时间MAMB, 等等。

Interpreting expressions using the logical connectives , , , and ¬ works exactly as it did in the propositional setting. MAB exactly when MA and MB, and so on.

我们仍然需要解释如何解释存在性和普遍性表达式。我们看到xA直观地意味着领域中存在一些元素,可以使A确实,当我们“替换”变量时x有了那个元素。为了更精确一点,我们说MxA当有一个元素时a在...的领域M当我们解释x作为a, 然后MA继续上面的例子,我们有Mx(R(x,b)),因为我们解释x我们有 6MR(x,b)

We still need to explain how to interpret existential and universal expressions. We saw that xA intuitively meant that there was some element of the domain that would make A true, when we “replaced” the variable x with that element. To make this a bit more precise, we say that MxA exactly when there is an element a in the domain of M such that, when we interpret x as a, then MA. To continue the example above, we have Mx(R(x,b)), since when we interpret x as 6 we have MR(x,b).

更简洁地说,我们可以说MxA当有一个a在...的领域M使得MA[a¯/x]. 符号A[a¯/x]表示每次发生xA已被符号取代a¯

More concisely, we can say that MxA when there is an a in the domain of M such that MA[a¯/x]. The notation A[a¯/x] indicates that every occurrence of x in A has been replaced by the symbol a¯.

最后,请记住,xA意味着A对于所有可能的值都成立x.我们通过以下方式明确这一点:MxA对于每个元素来说准确时间a在...的领域M、口译x作为a给出了MA。或者,我们可以说MxA当对于每一个a在...的领域M,我们有MA[a¯/x]在我们上面的例子中,Mx(R(x,b)),因为我们解释x因为 2 我们没有MR(x,b)

Finally, remember, xA means that A is true for all possible values of x. We make this precise by saying that MxA exactly when for every element a in the domain of M, interpreting x as a gives that MA. Alternatively, we can say that MxA when for every a in the domain of M, we have MA[a¯/x]. In our example above, Mx(R(x,b)), since when we interpret x as 2 we do not have MR(x,b).

这些规则使我们能够确定模型中任何句子的真值。(请记住,句子是没有自由变量的公式。)有一些微妙之处:例如,我们隐式地假设我们的公式不会对同一变量进行两次量化,就像xxA但大多数情况下,解释过程告诉我们“读”一个公式就像直接谈论领域中的对象一样。

These rules allow us to determine the truth value of any sentence in a model. (Remember, a sentence is a formula with no free variables.) There are some subtleties: for instance, we’ve implicitly assumed that our formula doesn’t quantify over the same variable twice, as in xxA. But for the most part, the interpretation process tells us to “read” a formula as talking directly about objects in the domain.

10.3.示例

10.3. Examples

采用一种没有常量符号、只有一个关系符号的简单语言和一个二元函数符号+. 我们的模型M将有域名N,并且符号将被解释为标准的小于或等于关系和加法函数。

Take a simple language with no constant symbols, one relation symbol , and one binary function symbol +. Our model M will have domain N, and the symbols will be interpreted as the standard less-than-or-equal-to relation and addition function.

在阅读以下答案之前,请先思考以下问题。请记住,我们的域名是N, 不是Z或任何其他数字系统。

Think about the following questions before you read the answers below. Remember, our domain is N, not Z or any other number system.

  1. 是真的吗Mx(xx)Mx(xx)

  2. Is it true that Mx(xx)? What about Mx(xx)?

  3. 同样地,Mx(x+xx)Mx(x+xx)

  4. Similarly, what about Mx(x+xx)? Mx(x+xx)?

  5. 做句子xy(xy)xy(xy)意思相同吗?它们是真的还是假的?

  6. Do the sentences xy(xy) and xy(xy) mean the same thing? Are they true or false?

  7. 你能想到一个公式吗A在这个语言中,有一个自由变量x,这样MxAMxA

  8. Can you think of a formula A in this language, with one free variable x, such that MxA but MxA?

这些问题表明了全称量词和存在量词之间存在微妙且通常很棘手的相互作用。一旦你对它们进行了一番思考,请阅读以下答案:

These questions indicate a subtle, and often tricky, interplay between the universal and existential quantifiers. Once you’ve thought about them a bit, read the answers:

  1. 这两个陈述都是正确的。对于前者,我们可以(例如)解释x作为自然数 0。那么,Mxx,所以存在性为真。对于后者,选择任意自然数n;情况仍然是这样的,当我们解释x作为n,我们有Mxx

  2. Both of these statements are true. For the former, we can (for example) interpret x as the natural number 0. Then, Mxx, so the existential is true. For the latter, pick an arbitrary natural number n; it is still the case that when we interpret x as n, we have Mxx.

  3. 第一个陈述是正确的,因为我们可以解释x为 0。但第二个陈述是错误的。当我们解释x为 1(或者,事实上,为 0 以外的任何自然数),我们看到Mx+xx

  4. The first statement is true, since we can interpret x as 0. The second statement, though, is false. When we interpret x as 1 (or, in fact, as any natural number besides 0), we see that Mx+xx.

  5. 这些句子的意思并不相同,尽管在指定的模型中,它们都是正确的。第一个句子表示某个自然数小于或等于每个自然数。这是正确的:0 小于或等于每个自然数。第二个句子说,对于每个自然数,都有另一个至少同样大的自然数。同样,这是正确的:每个自然数a小于或等于a。如果我们将我们的域名设为Z而不是N,第一句话就是假的,而第二句话仍然是正确的。

  6. These sentences do not mean the same thing, although in the specified model, both are true. The first expresses that some natural number is less than or equal to every natural number. This is true: 0 is less than or equal to every natural number. The second sentence says that for every natural number, there is another natural number at least as big. Again, this is true: every natural number a is less than or equal to a. If we took our domain to be Z instead of N, the first sentence would be false, while the second would still be true.

  7. 这里描述的情况在我们的模型中是不可能发生的。如果MxA, 然后MA[0¯/x],这意味着MxA。这种情况唯一可能发生的时候就是当我们的模型域为空的时候。

  8. The situation described here is impossible in our model. If MxA, then MA[0¯/x], which implies that MxA. The only time this situation can happen is when the domain of our model is empty.

现在考虑一种具有常量符号 2 的不同语言,谓词符号primeodd和二元关系<,以自然的方式解释域N. 这句话x(2<xprime(x)odd(x))表达了每个大于 2 的素数都是奇数的事实。这是相对化的一个例子,在第 7.4 节中讨论过。我们现在可以从语义上看到相对化的工作原理。如果对于每个自然数,这句话在我们的模型中是正确的n、口译x作为n使句子正确。如果我们解释x为 0、1 或 2,或任何非素数,则蕴涵的假设为假,因此2<xprime(x)odd(x)是真的。否则,如果我们解释x为大于 2 的素数,蕴涵式的假设和结论均为真,并且2<xprime(x)odd(x)又是正确的。因此,普遍陈述成立。正是这样的例子部分激发了我们在第 3 章中对蕴涵语义的理解;任何其他选择都会使相对化变得不可能。

Now consider a different language with constant symbol 2, predicate symbols prime and odd, and binary relation <, interpreted in the natural way over domain N. The sentence x(2<xprime(x)odd(x)) expresses the fact that every prime number bigger than 2 is odd. It is an example of relativization, discussed in Section 7.4. We can now see semantically how relativization works. This sentence is true in our model if, for every natural number n, interpreting x as n makes the sentence true. If we interpret x as 0, 1, or 2, or as any non-prime number, the hypothesis of the implication is false, and thus 2<xprime(x)odd(x) is true. Otherwise, if we interpret x as a prime number bigger than 2, both the hypothesis and conclusion of the implication are true, and 2<xprime(x)odd(x) is again true. Thus the universal statement holds. It was an example like this that partially motivated our semantics for implication back in Chapter 3; any other choice would make relativization impossible.

对于下一个示例,我们将考虑由“点”的矩形网格给出的模型。每个点都有颜色(红色、蓝色或绿色)和大小(小或大)。我们使用字母R代表一个大红点和r来表示一个小红点,同样地G,g,B,b

For the next example, we will consider models that are given by a rectangular grid of “dots.” Each dot has a color (red, blue, or green) and a size (small or large). We use the letter R to represent a large red dot and r to represent a small red dot, and similarly for G,g,B,b.

我们用来描述点世界的逻辑语言有谓词redgreenbluesmalllarge,这些解释都是显而易见的。关系adj(x,y)如果点引用的是xy是接触的,而不是对角线的。关系same-color(x,y)same-size(x,y)same-row(x,y), 和same-column(x,y)也是不言自明的。关系left-of(x,y)如果点引用的是x位于所指点的左边y,无论点位于哪一行。right-ofabove, 和below相似。

The logical language we use to describe our dot world has predicates red, green, blue, small and large, which are interpreted in the obvious ways. The relation adj(x,y) is true if the dots referred to by x and y are touching, not on a diagonal. The relations same-color(x,y), same-size(x,y), same-row(x,y), and same-column(x,y) are also self-explanatory. The relation left-of(x,y) is true if the dot referred to by x is left of the dot referred to by y, regardless of what rows the dots are in. The interpretations of right-of, above, and below are similar.

考虑以下句子:

Consider the following sentences:

  1. x(green(x)blue(x))

  2. x(green(x)blue(x))

  3. x,y(adj(x,y)green(x)green(y))

  4. x,y(adj(x,y)green(x)green(y))

  5. x((zright-of(z,x))(y(left-of(x,y)blue(y)small(y))))

  6. x((zright-of(z,x))(y(left-of(x,y)blue(y)small(y))))

  7. x(large(x)y(small(y)adj(x,y)))

  8. x(large(x)y(small(y)adj(x,y)))

  9. x(green(x)y(same-row(x,y)blue(y)))

  10. x(green(x)y(same-row(x,y)blue(y)))

  11. x,y(same-row(x,y)same-column(x,y)x=y)

  12. x,y(same-row(x,y)same-column(x,y)x=y)

  13. xy(adj(x,y)¬same-size(x,y))

  14. xy(adj(x,y)¬same-size(x,y))

  15. xy(adj(x,y)same-color(x,y))

  16. xy(adj(x,y)same-color(x,y))

  17. yx(adj(x,y)same-color(x,y))

  18. yx(adj(x,y)same-color(x,y))

  19. x(blue(x)y(green(y)above(x,y)))

  20. x(blue(x)y(green(y)above(x,y)))

我们可以在以下特定模型中对它们进行评估:

We can evaluate them in this particular model:

R

R

r

r

g

b

b

R

R

b

b

G

b

b

B

B

B

b

b

它们具有以下真值:

There they have the following truth values:

  1. 错误的

  2. false

  3. 真的

  4. true

  5. 真的

  6. true

  7. 错误的

  8. false

  9. 真的

  10. true

  11. 真的

  12. true

  13. 错误的

  14. false

  15. 真的

  16. true

  17. 错误的

  18. false

  19. 真的

  20. true

对于每个句子,看看你是否能找到一个模型使句子为真,另一个模型使句子为假。为了增加挑战性,尝试同时使所有句子为真。请注意,你可以使用任意数量的行和任意数量的列。

For each sentence, see if you can find a model that makes the sentence true, and another that makes it false. For an extra challenge, try to make all of the sentences true simultaneously. Notice that you can use any number of rows and any number of columns.

10.4.有效性和逻辑结果

10.4. Validity and Logical Consequence

我们已经看到,公式的真假往往取决于我们选择的模型。然而,有些公式在每个可能的模型中都是正确的。我们之前看到的一个例子是y(le(0,y)le(0,y))这句话为什么有效?假设M是语言的任意模型,假设a是域的任意元素M。 任何一个Mle(0,a¯)或者M¬le(0,a¯)无论如何,蕴涵的命题语义保证Mle(0,a¯)le(0,a¯).我们经常写A意思是A是有效的。

We have seen that whether a formula is true or false often depends on the model we choose. Some formulas, though, are true in every possible model. An example we saw earlier was y(le(0,y)le(0,y)). Why is this sentence valid? Suppose M is an arbitrary model of the language, and suppose a is an arbitrary element of the domain of M. Either Mle(0,a¯) or M¬le(0,a¯). In either case, the propositional semantics of implication guarantee that Mle(0,a¯)le(0,a¯). We often write A to mean that A is valid.

在命题设置中,有一种简单的方法可以确定公式是否是重言式。编写真值表并检查是否有任何以结尾的行F是算法性的,我们从一开始就知道真值表到底有多大。不幸的是,我们不能对一阶公式做同样的事情。任何语言都有无限多的模型,所以“一阶”真值表会无限长。更糟糕的是,即使检查一个公式在单个模型中是否为真也可能是一项非算法任务。要决定像xP(x)在具有无限域的模型中是正确的,我们可能必须检查P对于无数元素来说都是正确的。

In the propositional setting, there is an easy method to figure out if a formula is a tautology or not. Writing the truth table and checking for any rows ending with F is algorithmic, and we know from the beginning exactly how large the truth table will be. Unfortunately, we cannot do the same for first-order formulas. Any language has infinitely many models, so a “first-order” truth table would be infinitely long. To make matters worse, even checking whether a formula is true in a single model can be a non-algorithmic task. To decide whether a universal statement like xP(x) is true in a model with an infinite domain, we might have to check whether P is true of infinitely many elements.

这并不是说我们永远无法弄清楚一阶句子是否是重言式。例如,我们曾论证过y(lt(0,y)lt(0,y))就是其中之一。这只是一个比命题逻辑更难的问题。

This is not to say that we can never figure out if a first-order sentence is a tautology. For example, we have argued that y(lt(0,y)lt(0,y)) was one. It is just a more difficult question than for propositional logic.

就像命题逻辑的情况一样,我们可以将有效性的概念扩展为逻辑结果的概念。固定一阶语言,L。 认为Γ是一组句子L, 和A是一个句子L我们会说A 是合乎逻辑的结果 Γ如果每个模型ΓA. 这是一种表达方式A是“必然结果”A:在任何解释下,如果Γ成真了,A也同样如此。

As was the case with propositional logic, we can extend the notion of validity to a notion of logical consequence. Fix a first-order language, L. Suppose Γ is a set of sentences in L, and A is a sentence of L. We will say that A is a logical consequence of Γ if every model of Γ is a model of A. This is one way of spelling out that A is a “necessary consequence” of A: under any interpretation, if the hypotheses in Γ come out true, A is true as well.

10.5.健全性和完整性

10.5. Soundness and Completeness

在命题逻辑中,我们看到了可证明公式和重言式之间的密切联系——具体来说,一个公式是可证明的当且仅当它是重言式。更一般地,我们说一个公式A是一系列假设的逻辑结果,Γ,当且仅当存在自然演绎证明AΓ事实证明,类似的陈述对于一阶逻辑也成立。

In propositional logic, we saw a close connection between the provable formulas and the tautologies—specifically, a formula is provable if and only if it is a tautology. More generally, we say that a formula A is a logical consequence of a set of hypotheses, Γ, if and only if there is a natural deduction proof of A from Γ. It turns out that the analogous statements hold for first order logic.

“健全性”方向——事实上,如果A可以证明Γ然后A在任何模型中都是正确的Γ— 成立的理由与命题情况下成立的理由类似。具体来说,证明过程通过展示自然演绎的每条规则都保留了模型中的真理来进行。

The “soundness” direction—the fact that if A is provable from Γ then A is true in any model of Γ—holds for reasons that are similar to the reasons it holds in the propositional case. Specifically, the proof proceeds by showing that each rule of natural deduction preserves the truth in a model.

一阶逻辑的完备性定理最早由库尔特·哥德尔在其 1929 年的论文中证明。后来,莱昂·亨金给出了另一个更简单的证明。

The completeness theorem for first order logic was first proved by Kurt Gödel in his 1929 dissertation. Another, simpler proof was later provided by Leon Henkin.


定理。如果公式A是一组句子的逻辑结果Γ, 然后A可以证明Γ

Theorem. If a formula A is a logical consequence of a set of sentences Γ, then A is provable from Γ.


与命题逻辑版本相比,一阶完备性定理更难证明。我们不会在这里过多地讨论,但会指出一些主要思想。如果不能从这些假设中证明矛盾,则一组句子被称为是一致的。Henkin 证明中的大部分工作都是通过以下“模型存在”定理完成的:

Compared to the version for propositional logic, the first order completeness theorem is harder to prove. We will not go into too much detail here, but will indicate some of the main ideas. A set of sentences is said to be consistent if you cannot prove a contradiction from those hypotheses. Most of the work in Henkin’s proof is done by the following “model existence” theorem:


定理。每一个一致的句子集都有一个模型。

Theorem. Every consistent set of sentences has a model.


从这个定理很容易推导出完备性定理。假设没有证明AΓ.然后设置Γ{¬A}是一致的。(如果我们能证明Γ{¬A},然后根据归谬法规则,我们可以证明AΓ)根据模型存在定理,这意味着存在一个模型MΓ{¬A}. 但这是一个模型Γ这不是A,这意味着A不是逻辑结果Γ

From this theorem, it is easy to deduce the completeness theorem. Suppose there is no proof of A from Γ. Then the set Γ{¬A} is consistent. (If we could prove from Γ{¬A}, then by the reductio ad absurdum rule we could prove A from Γ.) By the model existence theorem, that means that there is a model M of Γ{¬A}. But this is a model of Γ that is not a model of A, which means that A is not a logical consequence of Γ.

模型存在定理的证明非常复杂。人们必须以某种方式从一组一致的句子中构建一个模型。策略是从句法实体构建模型,换句话说,使用扩展语言中的术语作为域的元素。

The proof of the model existence theorem is intricate. Somehow, from a consistent set of sentences, one has to build a model. The strategy is to build the model out of syntactic entities, in other words, to use terms in an expanded language as the elements of the domain.

这里的寓意与命题逻辑的寓意大致相同。因为我们在开发句法规则时已经考虑到了某种语义,所以两者展现了同一枚硬币的不同面:可证明的句子恰好是所有模型中都为真的句子,而从一组假设中可证明的句子恰好是这些假设的所有模型中都为真的句子。

The moral here is much the same as it was for propositional logic. Because we have developed our syntactic rules with a certain semantics in mind, the two exhibit different sides of the same coin: the provable sentences are exactly the ones that are true in all models, and the sentences that are provable from a set of hypotheses are exactly the ones that are true in all models of those hypotheses.

因此,我们有另一种方法来回答上一节提出的问题。要证明一个句子是有效的,没有必要在每个可能的模型中检查它的真实性。相反,只需给出一个证明就足够了。

We therefore have another way to answer the question posed in the previous section. To show that a sentence is valid, there is no need to check its truth in every possible model. Rather, it suffices to produce a proof.

10.6.练习

10.6. Exercises

  1. 在具有二元关系的一阶语言中,R(x,y),考虑以下句子:

    • xyR(x,y)

    • yxR(x,y)

    • x,y(R(x,y)xyz(R(x,z)R(z,y)xzyz))

    对于下列每个结构,确定每个句子是真还是假。

    • 结构(N,),即自然数中的解释R

    • 结构(Z,)

    • 结构(Q,)

    • 结构(N,),即自然数中的解释R是“除法”关系

    • 结构(P(N),),即变量在自然数集上的范围的解释,其中R被解释为子集关系。

  2. In a first-order language with a binary relation, R(x,y), consider the following sentences:

    • xyR(x,y)

    • yxR(x,y)

    • x,y(R(x,y)xyz(R(x,z)R(z,y)xzyz))

    For each of the following structures, determine whether of each of those sentences is true or false.

    • the structure (N,), that is, the interpretation in the natural numbers where R is

    • the structure (Z,)

    • the structure (Q,)

    • the structure (N,), that is, the interpretation in the natural numbers where R is the “divides” relation

    • the structure (P(N),), that is, the interpretation where variables range over sets of natural numbers, where R is interpreted as the subset relation.

  3. 创建一个 4 x 4 的“点”世界,使以下所有句子都成立:

    • x(green(x)blue(x))

    • x,y(adj(x,y)green(x)green(y))

    • x(zright-of(z,x)y(left-of(x,y)blue(y)small(y)))

    • x(large(x)y(small(y)adj(x,y)))

    • x(green(x)y(same-row(x,y)blue(y)))

    • x,y(same-row(x,y)same-column(x,y)x=y)

    • xy(adj(x,y)¬same-size(x,y))

    • xy(adj(x,y)same-color(x,y))

    • yx(adj(x,y)same-color(x,y))

    • x(blue(x)y(green(y)above(x,y)))

  4. Create a 4 x 4 “dots” world that makes all of the following sentences true:

    • x(green(x)blue(x))

    • x,y(adj(x,y)green(x)green(y))

    • x(zright-of(z,x)y(left-of(x,y)blue(y)small(y)))

    • x(large(x)y(small(y)adj(x,y)))

    • x(green(x)y(same-row(x,y)blue(y)))

    • x,y(same-row(x,y)same-column(x,y)x=y)

    • xy(adj(x,y)¬same-size(x,y))

    • xy(adj(x,y)same-color(x,y))

    • yx(adj(x,y)same-color(x,y))

    • x(blue(x)y(green(y)above(x,y)))

  5. 修复一阶语言L,并让AB是任意两个句子L. 请记住A意味着A是有效的。解开定义,表明如果AB, 然后AB

  6. Fix a first-order language L, and let A and B be any two sentences in L. Remember that A means that A is valid. Unpacking the definitions, show that if AB, then A and B.

  7. 举一个具体的例子来说明AB并不一定意味着A或者B换句话说,选择一种语言L并选择特定的句子AB使得AB有效,但A也不B是有效的。

  8. Give a concrete example to show that AB does not necessarily imply A or B. In other words, pick a language L and choose particular sentences A and B such that AB is valid but neither A nor B is valid.

  9. 考虑三个公式xR(x,x)xy(R(x,y)R(y,x)), 和xyz(R(x,y)R(y,z)R(x,z)).这些句子说R具有反身性、对称性和可训练性。对于每对句子,找到一个模型,使这两个句子为真,第三个句子为假。这表明这些句子在逻辑上是独立的:没有一个句子被其他句子所蕴涵。

  10. Consider the three formulas xR(x,x), xy(R(x,y)R(y,x)), and xyz(R(x,y)R(y,z)R(x,z)). These sentences say that R is reflexive, symmetric, and trainsitive. For each pair of sentences, find a model that makes those two sentences true and the third false. This shows that these sentences are logically independent: no one is entailed by the others.

  11. 证明如果一组公式{ψ1,,ψn}语义上不一致,那么它必然导致每个公式ϕ. 反之是否也成立?

  12. Show that if a set of formulas {ψ1,,ψn} is semantically inconsistent, then it entails every formula ϕ. Does the converse also hold?

  13. 给出一个公式ψ使得集合{P(c),¬P(D),ψ}是一致的,集合也是一致的{P(c),¬P(D),¬ψ}

  14. Give a formula ψ such that the set {P(c),¬P(D),ψ} is consistent, and so is the set {P(c),¬P(D),¬ψ}.

  15. 对于下列每个公式,说明该公式是否有效、可满足或不可满足。

    • xyR(y,x)R(x,y)

    • (xyR(x,y))(xyR(x,y))

    • (xP(x))(x¬P(x))

  16. For each the following formulas, show whether the formula is valid, satisfiable, or unsatisfiable.

    • xyR(y,x)R(x,y)

    • (xyR(x,y))(xyR(x,y))

    • (xP(x))(x¬P(x))

11.集合

11. Sets

我们已经到了这本教科书的转折点。从今以后,我们将在很大程度上放弃自然演绎,而专注于普通的数学证明。我们将继续思考如何用符号来表示非正式数学,以及自然演绎规则如何在非正式环境中发挥作用。但重点将放在编写普通数学论证上,而不是设计证明树。精益将继续充当非正式和正式领域之间的桥梁。

We have come to a turning point in this textbook. We will henceforth abandon natural deduction, for the most part, and focus on ordinary mathematical proofs. We will continue to think about how informal mathematics can be represented in symbolic terms, and how the rules of natural deduction play out in the informal setting. But the emphasis will be on writing ordinary mathematical arguments, not designing proof trees. Lean will continue to serve as a bridge between the informal and formal realms.

在本章中,我们将讨论在数学推理中发挥着基础性作用的一个概念,即“集合”。

In this chapter, we consider a notion that has come to play a fundamental role in mathematical reasoning, namely, that of a “set.”

11.1.初等集合论

11.1. Elementary Set Theory

1895 年,德国数学家格奥尔格·康托 (Georg Cantor) 在《数学年鉴》杂志上发表的一篇文章中,对集合(或用他的术语称为Menge )的概念进行了如下描述:

In a publication in the journal Mathematische Annalen in 1895, the German mathematician Georg Cantor presented the following characterization of the notion of a set (or Menge, in his terminology):

集合是指我们的直觉或思想中的确定的、不同的对象(称为 M 的元素)的任意集合 M,它们共同构成一个整体。

By a set we mean any collection M of determinate, distinct objects (called the elements of M) of our intuition or thought into a whole.

从那时起,集合的概念就被用来统一各种抽象和构造。我们将在后面的章节中讨论公理集合论,它为数学提供了基础,在数学中,一切都可以看作一个集合。

Since then, the notion of a set has been used to unify a wide range of abstractions and constructions. Axiomatic set theory, which we will discuss in a later chapter, provides a foundation for mathematics in which everything can be viewed as a set.

从广义上讲,任何集合都可以是一个集合;例如,我们可以考虑以 Ringo Star(数字 7)为元素的集合,以及以帝国大厦为唯一成员的集合。对于如此广泛的集合概念,我们必须小心:罗素悖论让我们考虑集合S所有不是自身元素的集合,这导致了一个矛盾,当我们问S是它自身的一个元素。(试试看!)集合论的公理告诉我们哪些集合存在,并且经过精心设计,以避免出现像罗素悖论那样的矛盾集合。

On a broad construal, any collection can be a set; for example, we can consider the set whose elements are Ringo Star, the number 7, and the set whose only member is the Empire State Building. With such a broad notion of set we have to be careful: Russell’s paradox has us consider the set S of all sets that are not elements of themselves, which leads to a contradiction when we ask whether S is an element of itself. (Try it!) The axioms of set theory tell us which sets exist, and have been carefully designed to avoid paradoxical sets like that of the Russell paradox.

实际上,数学家在使用集合时并没有那么随心所欲。通常,人们会固定一个域(例如自然数),然后考虑该域的子集。换句话说,我们考虑的是数字集、点集、线集等等,而不是任意的“集合”。在本文中,我们将采用这种惯例:当我们谈论集合时,我们总是隐含地谈论某个域的元素集。

In practice, mathematicians are not so freewheeling in their use of sets. Typically, one fixes a domain such as the natural numbers, and consider subsets of that domain. In other words, we consider sets of numbers, sets of points, sets of lines, and so on, rather than arbitrary “sets.” In this text, we will adopt this convention: when we talk about sets, we are always implicitly talking about sets of elements of some domain.

给定一个集合A某个域中的对象和某个对象x,我们写xAx是...的一个元素A康托尔的描述表明,每当我们拥有一些财产时,P,我们可以形成具有该属性的元素集。这用“集合构造器符号”表示为{xP(x)}例如,我们可以考虑以下所有自然数集:

Given a set A of objects in some domain and an object x, we write xA to say that x is an element of A. Cantor’s characterization suggests that whenever we have some property, P, of a domain, we can form the set of elements that have that property. This is denoted using “set-builder notation” as {xP(x)}. For example, we can consider all the following sets of natural numbers:

  • {nn is even}

  • {nn is even}

  • {nn is prime}

  • {nn is prime}

  • {nn is prime and greater than 2}

  • {nn is prime and greater than 2}

  • {nn can be written as a sum of squares}

  • {nn can be written as a sum of squares}

  • {nn is equal to 1, 2, or 3}

  • {nn is equal to 1, 2, or 3}

最后一组写得更简单{1,2,3}如果上下文中定义域不清楚,我们可以通过显式地写出它来指定它,例如在表达式中{nNn is even}

This last set is written more simply {1,2,3}. If the domain is not clear from the context, we can specify it by writing it explicitly, for example, in the expression {nNn is even}.

使用集合构造符号,我们可以定义许多常见的集合和运算。空,是没有元素的集合:

Using set-builder notation, we can define a number of common sets and operations. The empty set, , is the set with no elements:

={xfalse}.

对偶地,我们可以定义全集U,为定义域中每个元素组成的集合:

Dually, we can define the universal set, U, to be the set consisting of every element of the domain:

U={xtrue}.

给定两个集合AB,我们将它们的集定义为以下任一项中的元素集:

Given two sets A and B, we define their union to be the set of elements in either one:

AB={xxA or xB}.

我们将它们的交集定义为两者的元素集合:

And we define their intersection to be the set of elements of both:

AB={xxA and xB}.

我们定义A是不属于的元素集A

We define the complement of a set of A to be the set of elements that are not in A:

A={xxA}.

我们定义两个集合的集合差AB是元素的集合A但不是B

We define the set difference of two sets A and B to be the set of elements in A but not B:

AB={xxA and xB}.

如果两个集合有完全相同的元素,则称它们相等。如果AB是集合,A被称为B,书面AB,如果A是...的一个元素B。请注意A等于B当且仅当ABBA

Two sets are said to be equal if they have exactly the same elements. If A and B are sets, A is said to be a subset of B, written AB, if every element of A is an element of B. Notice that A is equal to B if and only if A is a subset of B and B is a subset of A.

还要注意,到目前为止我们所说的关于集合的一切都可以用符号逻辑来表示。我们可以将基本集合和构造函数的定义属性呈现如下:

Notice also that everything we have said about sets so far is readily representable in symbolic logic. We can render the defining properties of the basic sets and constructors as follows:

  • x(x)

  • x(x)

  • x(xU)

  • x(xU)

  • x(xABxAxB)

  • x(xABxAxB)

  • x(xABxAxB)

  • x(xABxAxB)

  • x(xAxA)

  • x(xAxA)

  • x(xABxAxB)

  • x(xABxAxB)

断言AB可以写成x(xAxB),以及断言A等于B可以写成x(xAxB)。这些都是全称陈述,即前面有全称量词,后面跟着基本断言和命题联结词的陈述。这意味着,形式上对集合的推理通常只不过是使用全称量词的规则和命题逻辑的规则。

The assertion that A is a subset of B can be written x(xAxB), and the assertion that A is equal to B can be written x(xAxB). These are all universal statements, that is, statements with universal quantifiers in front, followed by basic assertions and propositional connectives. What this means is that reasoning about sets formally often amounts to using nothing more than the rules for the universal quantifier together with the rules for propositional logic.

逻辑学家有时将普通的数学证明描述为非正式的,与自然演绎中的形式化证明相对。在编写非正式证明时,重点是可读性。以下是一个例子。

Logicians sometimes describe ordinary mathematical proofs as informal, in contrast to the formal proofs in natural deduction. When writing informal proofs, the focus is on readability. Here is an example.


定理。AB, 和C表示某个域的元素集。然后A(BC)=(AB)(AC)

Theorem. Let A, B, and C denote sets of elements of some domain. Then A(BC)=(AB)(AC).

证明。x任意,并假设x位于A(BC)。 然后x位于A,并且x位于B或者x位于C在第一种情况下,x位于AB,因此AB.在第二种情况下,x位于AC,因此AC无论如何,我们都有x位于(AB)(AC)

Proof. Let x be arbitrary, and suppose x is in A(BC). Then x is in A, and either x is in B or x is in C. In the first case, x is in A and B, and hence in AB. In the second case, x is in A and C, and hence AC. Either way, we have that x is in (AB)(AC).

相反,假设x位于(AB)(AC)。目前有两起案件。

Conversely, suppose x is in (AB)(AC). There are now two cases.

首先假设x位于AB。 然后x两者都是AB。 自从x位于B,它也位于BC, 所以x位于A(BC)

First, suppose x is in AB. Then x is in both A and B. Since x is in B, it is also in BC, and so x is in A(BC).

第二种情况类似:假设x位于AC。 然后x两者都是AC,同样如此BC。因此,在这种情况下,x位于A(BC),按要求。

The second case is similar: suppose x is in AC. Then x is in both A and C, and so also in BC. Hence, in this case also, x is in A(BC), as required.


请注意,这个证明看起来一点也不像符号逻辑中的证明。首先,普通的证明倾向于使用文字而不是符号。当然,数学总是使用符号,但不是代替“与”和“非”这样的文字;你很少甚至从未见过这些符号¬在数学教科书中,除非它是专门关于逻辑的教科书。

Notice that this proof does not look anything like a proof in symbolic logic. For one thing, ordinary proofs tend to favor words over symbols. Of course, mathematics uses symbols all the time, but not in place of words like “and” and “not”; you will rarely, if ever, see the symbols and ¬ in a mathematics textbook, unless it is a textbook specifically about logic.

同样,非正式证明的结构是通过普通的段落和标点符号来传达的。不要依赖图形图表、换行符和缩进来传达证明的结构。相反,你应该依赖文学手法,如路标和预示。在深入细节之前,提出证明的大纲或关键思想通常会有所帮助,段落的引言可以帮助引导读者的期望,就像在说明性文章中一样。

Similarly, the structure of an informal proof is conveyed with ordinary paragraphs and punctuation. Don’t rely on pictorial diagrams, line breaks, and indentation to convey the structure of a proof. Rather, you should rely on literary devices like signposting and foreshadowing. It is often helpful to present an outline of a proof or the key ideas before delving into the details, and the introductory sentence of a paragraph can help guide a reader’s expectations, just as it does in an expository essay.

尽管如此,你应该能够在上面的证明中隐含地看到自然演绎的元素。从形式上讲,该定理等同于断言

Nonetheless, you should be able to see elements of natural deduction implicitly in the proof above. In formal terms, the theorem is equivalent to the assertion

x(xA(BC)x(AB)(AC)),

证明也同样进行。短语“让x任意”是代码引入规则,其余证明的形式是介绍。说x位于A(BC)隐含地是一个“和”,并且论证使用消除得到xAxBC. 话说xBC隐含地是一个“或”,然后证明会根据情况进行拆分,取决于xB或者xC

and the proof proceeds accordingly. The phrase “let x be arbitrary” is code for the introduction rule, and the form of the rest of the proof is a introduction. Saying that x is in A(BC) is implicitly an “and,” and the argument uses elimination to get xA and xBC. Saying xBC is implicitly an “or,” and the proof then splits on cases, depending on whether xB or xC.

以“与”和“或”的形式展开交集和并集的定义,先前证明的“仅当”方向可以用自然演绎来表示,如下:

Modulo the unfolding of definition of intersection and union in terms of “and” and “or,” the “only if” direction of the previous proof could be represented in natural deduction like this:

在下一章中,我们将看到这种逻辑结构在 Lean 中得到体现。但是,用自然演绎法写出长篇证明并不是传达数学思想的最有效方式。因此,我们的目标是教你用自然演绎规则来思考,但用普通英语来表达步骤。

In the next chapter, we will see that this logical structure is made manifest in Lean. But writing long proofs in natural deduction is not the most effective to communicate the mathematical ideas. So our goal here is to teach you to think in terms of natural deduction rules, but express the steps in ordinary English.

这是另一个例子。

Here is another example.


定理。 (AB)C=A(BC)

Theorem. (AB)C=A(BC).

证明。x任意,并假设x位于(AB)C。 然后x位于AB但不是C,因此它在A但不是在B或者C.这意味着x位于A但不是BC等等A(BC)

Proof. Let x be arbitrary, and suppose x is in (AB)C. Then x is in AB but not C, and hence it is in A but not in B or C. This means that x is in A but not BC, and so in A(BC).

相反,假设x位于A(BC)。 然后x位于A,但不是在BC。 尤其,x既不属于B也不C,因为否则它将在BC。 所以x位于AB,因此(AB)C

Conversely, suppose x is in A(BC). Then x is in A, but not in BC. In particular, x is in neither B nor C, because otherwise it would be in BC. So x is in AB, and hence in (AB)C.


非正式证明和正式证明之间最大的区别可能在于细节程度。非正式证明通常会跳过那些被认为“直截了当”或“显而易见”的细节,而花更多精力阐明新颖或出乎意料的推论。

Perhaps the biggest difference between informal proofs and formal proofs is the level of detail. Informal proofs will often skip over details that are taken to be “straightforward” or “obvious,” devoting more effort to spelling out inferences that are novel or unexpected.

写出好的证明就像写出好的论文一样。要让读者相信结论是正确的,你必须让他们理解论点,而不要用不必要的细节让他们不知所措。在心中有一个特定的受众会有所帮助。试着向朋友、室友和家人大声说出你的论点;如果他们的眼睛呆滞,那么期望匿名读者做得更好是不合理的。

Writing a good proof is like writing a good essay. To convince your readers that the conclusion is correct, you have to get them to understand the argument, without overwhelming them with unnecessary details. It helps to have a specific audience in mind. Try speaking the argument aloud to friends, roommates, and family members; if their eyes glaze over, it is unreasonable to expect anonymous readers to do better.

学习写好证明的最好方法之一是阅读好的证明,并注意写作风格。挑选一本你认为特别清晰和吸引人的教科书的例子,思考是什么让它如此。

One of the best ways to learn to write good proofs is to read good proofs, and pay attention to the style of writing. Pick an example of a textbook that you find especially clear and engaging, and think about what makes it so.

自然演绎和形式验证可以帮助您理解使证明正确的组成部分,但您必须培养一种直觉,以了解什么使证明易于阅读且令人愉快。

Natural deduction and formal verification can help you understand the components that make a proof correct, but you will have to develop an intuitive feel for what makes a proof easy and enjoyable to read.

11.2.使用集合进行计算

11.2. Calculations with Sets

计算是数学的核心,数学证明通常涉及执行一系列计算。事实上,计算本身可以看作是两个表达式描述同一实体的证明。

Calculation is a central to mathematics, and mathematical proofs often involve carrying out a sequence of calculations. Indeed, a calculation can be viewed as a proof in and of itself that two expressions describe the same entity.

在高中代数中,学生经常被要求证明如下恒等式:

In high school algebra, students are often asked to prove identities like the following:


主张。 n(n+1)2+(n+1)=(n+1)(n+2)2对于每个自然数n

Proposition. n(n+1)2+(n+1)=(n+1)(n+2)2, for every natural number n.


在某些地方,要求学生写这样的证明:

In some places, students are asked to write proofs like this:


证明。

Proof.

n(n+1)2+(n+1)=?(n+1)(n+2)2n2+n2+2n+22=?n2+3n+22n2+n+2n+22=?n2+3n+22n2+3n+22=n2+3n+22.

数学家们看到这种情况时通常会感到畏缩。不要这样做!这看起来像是正向推理的一个例子,我们从一个复杂的身份开始,最终证明x=x当然,真正的意思是每行都与下一行相接。有一种表达方式,用短语“足以说明”。以下表示更接近数学术语:

Mathematicians generally cringe when they see this. Don’t do it! It looks like an instance of forward reasoning, where we start with a complex identity and end up proving x=x. Of course, what is really meant is that each line follows from the next. There is a way of expressing this, with the phrase “it suffices to show.” The following presentation comes closer to mathematical vernacular:


证明。我们想证明

Proof. We want to show

n(n+1)2+(n+1)=(n+1)(n+2)2.

要做到这一点,只需证明

To do that, it suffices to show

n2+n2+2n+22=n2+3n+22.

为此,只需证明

For that, it suffices to show

n2+n+2n+22=n2+3n+22.

但最后一个等式显然是正确的。

But this last equation is clearly true.


然而,这种叙述不太流畅。有时在证明中采用逆向推理是有充分理由的,但在这种情况下,以更直接的方式呈现证明更容易。以下是一个例子:

The narrative doesn’t flow well, however. Sometimes there are good reasons to work backward in a proof, but in this case it is easy to present the proof in a more forward-directed manner. Here is one example:


证明。计算左边,我们有

Proof. Calculating on the left-hand side, we have

n(n+1)2+(n+1)=n2+n2+2n+22=n2+n+2n+22=n2+3n+22.

在右侧,我们还有

On the right-hand side, we also have

(n+1)(n+2)2=n2+3n+22.

所以n(n+1)2+(n+1)=n2+3n+22,按要求。

So n(n+1)2+(n+1)=n2+3n+22, as required.


在这种情况下,数学家经常使用缩写“LHS”和“RHS”分别表示“左侧”和“右侧”。事实上,在这里我们可以轻松地将证明写成一个前向计算:

Mathematicians often use the abbreviations “LHS” and “RHS” for “left-hand side” and “right-hand side,” respectively, in situations like this. In fact, here we can easily write the proof as a single forward-directed calculation:


证明。

Proof.

n(n+1)2+(n+1)=n2+n2+2n+22=n2+n+2n+22=n2+3n+22=(n+1)(n+2)2.

这样的证明清晰、紧凑且易于阅读。读者面临的主要挑战是弄清楚每个后续步骤的合理性。数学家有时会用附加信息注释此类计算,或在文本前后添加几句解释。但理想的情况是将计算分成足够小的步骤,以便每个步骤都很简单,不需要解释。(同样,什么才是“简单”取决于阅读证明的人。)

Such a proof is clear, compact, and easy to read. The main challenge to the reader is to figure out what justifies each subsequent step. Mathematicians sometimes annotate such a calculation with additional information, or add a few words of explanation in the text before and/or after. But the ideal situation is to carry out the calculation in small enough steps so that each step is straightforward, and needs no explanation. (And, once again, what counts as “straightforward” will vary depending on who is reading the proof.)

我们说过,如果两个集合具有相同的元素,则它们相等。在上一节中,我们通过推理每个集合的元素来证明两个集合相等,但我们通常可以更高效。假设AB, 和C是某个域的子集U,下列恒等式成立:

We have said that two sets are equal if they have the same elements. In the previous section, we proved that two sets are equal by reasoning about the elements of each, but we can often be more efficient. Assuming A, B, and C are subsets of some domain U, the following identities hold:

  • AA=U

  • AA=U

  • AA=

  • AA=

  • A=A

  • A=A

  • AA=A

  • AA=A

  • AA=A

  • AA=A

  • A=A

  • A=A

  • A=

  • A=

  • AU=U

  • AU=U

  • AU=A

  • AU=A

  • AB=BA

  • AB=BA

  • AB=BA

  • AB=BA

  • (AB)C=A(BC)

  • (AB)C=A(BC)

  • (AB)C=A(BC)

  • (AB)C=A(BC)

  • AB=AB

  • AB=AB

  • AB=AB

  • AB=AB

  • A(BC)=(AB)(AC)

  • A(BC)=(AB)(AC)

  • A(BC)=(AB)(AC)

  • A(BC)=(AB)(AC)

  • A(AB)=A

  • A(AB)=A

  • A(AB)=A

  • A(AB)=A

这使我们能够通过计算来证明进一步的身份。以下是一个例子。

This allows us to prove further identities by calculating. Here is an example.


定理。设AB是某个域的子集U。 然后(AB)B=AB

Theorem. Let A and B be subsets of some domain U. Then (AB)B=AB.

证明

Proof.

(AB)B=(AB)(BB)=(AB)U=AB.

这是另一个例子。

Here is another example.


定理。设AB是某个域的子集U。 然后(AB)(BA)=(AB)(AB)

Theorem. Let A and B be subsets of some domain U. Then (AB)(BA)=(AB)(AB).

证明

Proof.

(AB)(BA)=(AB)(BA)=((AB)B)((AB)A)=((AB)(BB))((AA)(BA))=((AB)U)(UBA)=(AB)(AB)=(AB)(AB).

经典地,您可能已经注意到,在逻辑等价性下,命题满足与集合类似的恒等式。这并非巧合;两者都是布尔代数的实例。以下是将上述恒等式翻译成布尔代数语言:

Classically, you may have noticed that propositions, under logical equivalence, satisfy identities similar to sets. That is no coincidence; both are instances of boolean algebras. Here are the identities above translated to the language of a boolean algebra:

  • A¬A=

  • A¬A=

  • A¬A=

  • A¬A=

  • ¬¬A=A

  • ¬¬A=A

  • AA=A

  • AA=A

  • AA=A

  • AA=A

  • A=A

  • A=A

  • A=

  • A=

  • A=

  • A=

  • A=A

  • A=A

  • AB=BA

  • AB=BA

  • AB=BA

  • AB=BA

  • (AB)C=A(BC)

  • (AB)C=A(BC)

  • (AB)C=A(BC)

  • (AB)C=A(BC)

  • ¬(AB)=¬A¬B

  • ¬(AB)=¬A¬B

  • ¬(AB)=¬A¬B

  • ¬(AB)=¬A¬B

  • A(BC)=(AB)(AC)

  • A(BC)=(AB)(AC)

  • A(BC)=(AB)(AC)

  • A(BC)=(AB)(AC)

  • A(AB)=A

  • A(AB)=A

  • A(AB)=A

  • A(AB)=A

翻译成布尔代数的语言,上面的第一定理如下:

Translated to the language of boolean algebras, the first theorem above is as follows:


定理。AB是布尔代数的元素。然后(A¬B)B=B

Theorem. Let A and B be elements of a boolean algebra. Then (A¬B)B=B.

证明。

Proof.

(A¬B)B=(AB)(¬BB)=(AB)=(AB).

11.3.索引集合族

11.3. Indexed Families of Sets

如果I是一个集合,我们有时会希望考虑一个家庭 (Ai)iI由元素索引的集合I例如,我们可能对一个序列感兴趣

If I is a set, we will sometimes wish to consider a family (Ai)iI of sets indexed by elements of I. For example, we might be interested in a sequence

A0,A1,A2,

由自然数索引的集合。这个概念最好通过一些例子来说明。

of sets indexed by the natural numbers. The concept is best illustrated by some examples.

  • 对于每个自然数n,我们可以定义集合An是当今年龄在世的一群人n。对于每个年龄,我们都有相应的集合。20 岁的人是集合中的一个元素A20,而新生婴儿是A0. 该套装A200是空的。这个家庭(An)nN是一个以自然数为索引的集合族。

  • For each natural number n, we can define the set An to be the set of people alive today that are of age n. For each age we have the corresponding set. Someone of age 20 is an element of the set A20, while a newborn baby is an element of A0. The set A200 is empty. This family (An)nN is a is a family of sets indexed by the natural numbers.

  • 对于每个实数r我们可以定义Br是大于r, 所以Br={xRx>r and x>0}。 然后(Br)rR是一个以实数为索引的集合族。

  • For every real number r we can define Br to be the set of positive real numbers larger than r, so Br={xRx>r and x>0}. Then (Br)rR is a family of sets indexed by the real numbers.

  • 对于每个自然数n我们可以定义Cn={kNk is a divisor of n}作为除数的集合n

  • For every natural number n we can define Cn={kNk is a divisor of n} as the set of divisors of n.

有了家庭(Ai)iI集合索引为I,我们可以将其集:

Given a family (Ai)iI of sets indexed by I, we can form its union:

iIAi={xxAi for some iI}.

我们还可以形成集合族的交集:

We can also form the intersection of a family of sets:

iIAi={xxAi for every iI}.

因此一个元素x位于iIAi当且仅当x位于Ai对于一些 iI, 和x位于iIAi当且仅当x位于Ai对于每一个iI。这些操作在符号逻辑中由存在量词和全称量词表示。我们有:

So an element x is in iIAi if and only if x is in Ai for some i in I, and x is in iIAi if and only if x is in Ai for every i in I. These operations are represented in symbolic logic by the existential and the universal quantifiers. We have:

  • x(xiIAiiI(xAi))

  • x(xiIAiiI(xAi))

  • x(xiIAiiI(xAi))

  • x(xiIAiiI(xAi))

回到上面的例子,我们可以计算每个家族的并集和交集。对于第一个例子,nNAn是所有活着的人的集合,并且nNAn=。 还,rRBr=R>0,所有正实数的集合,以及rRBr=对于最后一个例子,我们有nNCn=NnNCn={1},因为 1 是每个自然数的除数。

Returning to the examples above, we can compute the union and intersection of each family. For the first example, nNAn is the set of all living people, and nNAn=. Also, rRBr=R>0, the set of all positive real numbers, and rRBr=. For the last example, we have nNCn=N and nNCn={1}, since 1 is a divisor of every natural number.

假设I只包含两个元素,比如I={c,d}。 让(Ai)iI是一个集合族,索引为I。 因为I有两个元素,这个家族只由两个集合组成AcAd那么这个家族的并集与交集就是两个集合的并集与交集:

Suppose that I contains just two elements, say I={c,d}. Let (Ai)iI be a family of sets indexed by I. Because I has two elements, this family consists of just the two sets Ac and Ad. Then the union and intersection of this family are just the union and intersection of the two sets:

iIAi=AcAdiIAi=AcAd.

这意味着两个集合的并集与交集只是集合族的并集与交集的一个特例。

This means that the union and intersection of two sets are just a special case of the union and intersection of a family of sets.

我们也有集合族的并集和交集的等式。下面是其中一些:

We also have equalities for unions and intersections of families of sets. Here are a few of them:

  • AiIBi=iI(ABi)

  • AiIBi=iI(ABi)

  • AiIBi=iI(ABi)

  • AiIBi=iI(ABi)

  • iIAi=iIAi

  • iIAi=iIAi

  • iIAi=iIAi

  • iIAi=iIAi

  • iIjJAi,j=jJiIAi,j

  • iIjJAi,j=jJiIAi,j

  • iIjJAi,j=jJiIAi,j

  • iIjJAi,j=jJiIAi,j

在最后两行中,Ai,j由两个集合索引IJ.这意味着对于每一个iIjJ我们有一套Ai,j对于前四个等式,尝试弄清楚如果索引集I包含两个元素。

In the last two lines, Ai,j is indexed by two sets I and J. This means that for every iI and jJ we have a set Ai,j. For the first four equalities, try to figure out what the rule means if the index set I contains two elements.

让我们证明第一个恒等式。注意断言的逻辑形式xAiIBixiI(ABi)决定证明的结构。

Let’s prove the first identity. Notice how the logical forms of the assertions xAiIBi and xiI(ABi) dictate the structure of the proof.


定理。A是某个域的任意子集U,并让(Bi)iI是子集家族U索引I。 然后

Theorem. Let A be any subset of some domain U, and let (Bi)iI be a family of subsets of U indexed by I. Then

AiIBi=iI(ABi).

证明。假设x位于AiIBi。 然后x位于Ax位于Bj对于一些jI。 所以x位于ABj,因此iI(ABi)

Proof. Suppose x is in AiIBi. Then x is in A and x is in Bj for some jI. So x is in ABj, and hence in iI(ABi).

相反,假设x位于iI(ABi)。然后,对于一些jIx位于ABj。 因此x位于A,并且x位于Bj,它在iIBi。 因此x位于AiIBi,按要求。

Conversely, suppose x is in iI(ABi). Then, for some j in I, x is in ABj. Hence x is in A, and since x is in Bj, it is in iIBi. Hence x is in AiIBi, as required.


11.4.笛卡尔积与幂集

11.4. Cartesian Product and Power Set

两个对象的有序ab表示为(a,b).我们说a第一个组件b是该对的第二个组成部分。只有当第一个组成部分相等且第二个组成部分相等时,两个对才相等。在符号中,(a,b)=(c,d)当且仅当a=cb=d

The ordered pair of two objects a and b is denoted (a,b). We say that a is the first component and b is the second component of the pair. Two pairs are only equal if the first component are equal and the second components are equal. In symbols, (a,b)=(c,d) if and only if a=c and b=d.

给定两个集合AB,我们定义笛卡尔积 A×B这两个集合是所有对的集合,其中第一个组件是A第二个组件是B. 在集合构造器符号中这意味着

Given two sets A and B, we define the cartesian product A×B of these two sets as the set of all pairs where the first component is an element in A and the second component is an element in B. In set-builder notation this means

A×B={(a,b)aA and bB}.

请注意,如果AB是特定域的子集U,集合A×B不必是同一域的子集。但是,它将是U×U

Note that if A and B are subsets of a particular domain U, the set A×B need not be a subset of the same domain. However, it will be a subset of U×U.

一些公理基础将对的概念视为原始概念。在公理集合论中,通常将有序对定义为特定集合,即

Some axiomatic foundations take the notion of a pair to be primitive. In axiomatic set theory, it is common to define an ordered pair to be a particular set, namely

(a,b)={{a},{a,b}}.

请注意,如果a=b,此集合只有一个元素:

Notice that if a=b, this set has only one element:

(a,a)={{a},{a,a}}={{a},{a}}={{a}}.

下面的定理表明这个定义是合理的。

The following theorem shows that this definition is reasonable.


定理。利用上面有序对的定义,我们有(a,b)=(c,d)当且仅当a=cb=d

Theorem. Using the definition of ordered pairs above, we have (a,b)=(c,d) if and only if a=c and b=d.

证明。如果a=cb=d那么显然(a,b)=(c,d)。对于另一个方向,假设(a,b)=(c,d),这意味着

Proof. If a=c and b=d then clearly (a,b)=(c,d). For the other direction, suppose that (a,b)=(c,d), which means

{{a},{a,b}}L={{c},{c,d}}R.

首先假设a=b。 然后L={{a}}.这意味着{c}={a}{c,d}={a},由此我们得出结论c=ad=a=b

Suppose first that a=b. Then L={{a}}. This means that {c}={a} and {c,d}={a}, from which we conclude that c=a and d=a=b.

现在假设ab。 如果{c}={a,b}那么我们得出结论ab都等于c,矛盾的ab。 自从{c}L{c}必须等于{a},这意味着a=c我们知道{a,b}R,因为我们知道{a,b}{c},我们得出结论{a,b}={c,d}.这意味着b{c,d}, 自从ba=c,我们得出的结论是b=d

Now suppose that ab. If {c}={a,b} then we conclude that a and b are both equal to c, contradicting ab. Since {c}L, {c} must be equal to {a}, which means that a=c. We know that {a,b}R, and since we know {a,b}{c}, we conclude {a,b}={c,d}. This means that b{c,d}, since ba=c, we conclude that b=d.

因此,在这两种情况下,我们得出的结论是a=cb=d,证明了定理。

Hence in both cases we conclude that a=c and b=d, proving the theorem.


利用有序对,我们可以定义有序三元组 (a,b,c)(a,(b,c)).那么我们可以证明(a,b,c)=(d,e,f)当且仅当a=db=ec=f,这是练习中要求你做的。我们还可以定义有序n-元组,是n对象,以类似的方式。

Using ordered pairs we can define the ordered triple (a,b,c) to be (a,(b,c)). Then we can prove that (a,b,c)=(d,e,f) if and only if a=d, b=e and c=f, which you are asked to do in the exercises. We can also define ordered n-tuples, which are sequence of n objects, in a similar way.

给定一个集合A我们可以定义幂集 P(A)是所有子集的集合A. 在集合构造符号中,我们可以将其写为

Given a set A we can define the power set P(A) to be the set of all subsets of A. In set-builder notation we can write this as

P(A)={BBA}.

如果AUP(A)可能不是U,但它始终是P(U)

If A is a subset of U, P(A) may not be a subset of U, but it is always a subset of P(U).

11.5.练习

11.5. Exercises

  1. 证明以下定理:设AB, 和C是某个域的元素集。那么A(BC)=(AB)(AC).(从今以后,如果我们不指定自然演绎或精益,“证明”和“展示”意味着给出一个普通的数学证明,使用普通的数学语言而不是符号逻辑。)

  2. Prove the following theorem: Let A, B, and C be sets of elements of some domain. Then A(BC)=(AB)(AC). (Henceforth, if we don’t specify natural deduction or Lean, ``prove’’ and ``show’’ mean give an ordinary mathematical proof, using ordinary mathematical language rather than symbolic logic.)

  3. 证明以下定理:设AB是某个域的元素集。那么AB=AB

  4. Prove the following theorem: Let A and B be sets of elements of some domain. Then AB=AB.

  5. 两套AB如果它们没有共同元素,则称它们不相交。说明如果AB是不相交的,CA, 和DB, 然后CD是不相交的。

  6. Two sets A and B are said to be disjoint if they have no element in common. Show that if A and B are disjoint, CA, and DB, then C and D are disjoint.

  7. AB是集合。显示(AB)(BA)=(AB)(AB),通过表明两边具有相同的元素。

  8. Let A and B be sets. Show (AB)(BA)=(AB)(AB), by showing that both sides have the same elements.

  9. AB, 和C是某个域的子集U. 给出恒等式的计算证明A(BC)=(AB)C,使用上面的恒等式。还可以利用以下事实:一般来说,CD=CD

  10. Let A, B, and C be subsets of some domain U. Give a calculational proof of the identity A(BC)=(AB)C, using the identities above. Also use the fact that, in general, CD=CD.

  11. 类似地,给出计算证明(AB)(AB)=A

  12. Similarly, give a calculational proof of (AB)(AB)=A.

  13. 给出下列计算证明:

    • AB=A(AB)

    • AB=(AB)B

    • (AB)C=(AC)B

  14. Give calculational proofs of the following:

    • AB=A(AB)

    • AB=(AB)B

    • (AB)C=(AC)B

  15. 证明如果(Ai,j)iI,jJ是一个由两个集合索引的家庭IJ, 然后

    iIjJAi,jjJiIAi,j.

    另外,找一个家庭(Ai,j)iI,jJ其中逆包含不成立。

  16. Prove that if (Ai,j)iI,jJ is a family indexed by two sets I and J, then

    iIjJAi,jjJiIAi,j.

    Also, find a family (Ai,j)iI,jJ where the reverse inclusion does not hold.

  17. 使用计算推理证明

    (iIAi)(jJBj)=iIjJ(AiBj).

    符号iIjJ(AiBj)方法iIjJ(AiBj)

  18. Prove using calculational reasoning that

    (iIAi)(jJBj)=iIjJ(AiBj).

    The notation iIjJ(AiBj) means iIjJ(AiBj).

  19. 使用定义(a,b,c)=(a,(b,c)),表明(a,b,c)=(d,e,f)当且仅当a=db=ec=f

  20. Using the definition (a,b,c)=(a,(b,c)), show that (a,b,c)=(d,e,f) if and only if a=d, b=e and c=f.

  21. 证明A×(BC)=(A×B)(A×C)

  22. Prove that A×(BC)=(A×B)(A×C)

  23. 证明(AB)×(CD)=(A×C)(B×D). 找到一个表达方式(AB)×(CD)由笛卡尔积的并集组成,并证明你的表达是正确的。

  24. Prove that (AB)×(CD)=(A×C)(B×D). Find an expression for (AB)×(CD) consisting of unions of cartesian products, and prove that your expression is correct.

  25. 证明AB当且仅当P(A)P(B)

  26. Prove that that AB if and only if P(A)P(B).

12.精益中的集合

12. Sets in Lean

在上一章中,我们注意到,虽然在公理集合论中人们考虑的是不同对象的集合,但在数学中更常见的是考虑某个固定域的子集,U。这是 Lean 处理集合的方式。对于任何数据类型U,Lean 都会为我们提供一种新的数据类型 ,它由 的元素集组成。因此,例如,我们可以推理自然数集、整数集或自然数对集。set UU

In the last chapter, we noted that although in axiomatic set theory one considers sets of disparate objects, it is more common in mathematics to consider subsets of some fixed domain, U. This is the way sets are handled in Lean. For any data type U, Lean gives us a new data type, set U, consisting of the sets of elements of U. Thus, for example, we can reason about sets of natural numbers, or sets of integers, or sets of pairs of natural numbers.

12.1.基础知识

12.1. Basics

给定和,我们可以写出表示是集合的成员。可以使用来输入字符。A : set Ux : Ux AxA\in

Given A : set U and x : U, we can write x A to state that x is a member of the set A. The character can be typed using \in.

import data.set
open set

variable {U : Type}
variables A B C : set U
variable x : U

#check x  A
#check A  B
#check B \ C
#check C  A
#check C
#check   A
#check B  univ
import data.set
open set

variable {U : Type}
variables A B C : set U
variable x : U

#check x  A
#check A  B
#check B \ C
#check C  A
#check C
#check   A
#check B  univ

您可以将符号、分别输入为、、和。我们将类型变量设为隐式,因为它通常可以从上下文中推断出来。全集表示为,集合补集用上标字母“c”表示,您可以将其输入为或。类似这些的基本集合论概念在 Lean 的核心库中定义,但辅助库中提供了其他定理和符号,我们使用命令 加载了该辅助库,该命令必须出现在文件的开头。该命令让我们引用名为的定理。\\subeq \empty\un\i\\Uuniv\^c\complimport data.setopen setset.mem_unionmem_union

You can type the symbols , , , , \ as \subeq \empty, \un, \i, and \\, respectively. We have made the type variable U implicit, because it can typically be inferred from context. The universal set is denoted univ, and set complementation is denoted with the superscripted letter “c,” which you can enter as \^c or \compl. Basic set-theoretic notions like these are defined in Lean’s core library, but additional theorems and notation are available in an auxiliary library that we have loaded with the command import data.set, which has to appear at the beginning of a file. The command open set lets us refer to a theorem named set.mem_union as mem_union.

下面的模式可以用来表明A是的子集B

The following pattern can be used to show that A is a subset of B:

example : A  B :=
assume x,
assume h : x  A,
show x  B, from sorry
example : A  B :=
assume x,
assume h : x  A,
show x  B, from sorry

并且可以使用以下模式来证明AB相等:

And the following pattern can be used to show that A and B are equal:

example : A = B :=
eq_of_subset_of_subset
  (assume x,
    assume h : x  A,
    show x  B, from sorry)
  (assume x,
    assume h : x  B,
    show x  A, from sorry)
example : A = B :=
eq_of_subset_of_subset
  (assume x,
    assume h : x  A,
    show x  B, from sorry)
  (assume x,
    assume h : x  B,
    show x  A, from sorry)

或者,我们可以使用以下模式:

Alternatively, we can use the following pattern:

example : A = B :=
ext (assume x, iff.intro
  (assume h : x  A,
    show x  B, from sorry)
  (assume h : x  B,
    show x  A, from sorry))
example : A = B :=
ext (assume x, iff.intro
  (assume h : x  A,
    show x  B, from sorry)
  (assume h : x  B,
    show x  A, from sorry))

这里,ext是“外延性”的简称。用符号表示,就是以下事实:

Here, ext is short for “extensionality.” In symbolic terms, it is the following fact:

x(xAxB)A=B.

这减少了证明A=B证明x(xAxB),我们可以使用介绍。

This reduces proving A=B to proving x(xAxB), which we can do using and introduction.

此外,Lean 支持以下巧妙的功能:集合上的并集、交集和其他运算的定义规则被认为是“定义性”的。这意味着表达式和对 Lean 来说含义相同。对于集合上的其他构造也是如此;例如和对 Lean 含义相同。您还可以为编写,其中使用 编写。对于其他集合构造,上一章中的定义等价关系定义性地成立。以下示例说明了这些功能。x A Bx A x Bx A \ Bx A ¬ (x B)x B¬ (x B)\notin

Moreover, Lean supports the following nifty feature: the defining rules for union, intersection and other operations on sets are considered to hold “definitionally.” This means that the expressions x A B and x A x B mean the same thing to Lean. This is the same for the other constructions on sets; for example x A \ B and x A ¬ (x B) mean the same thing to Lean. You can also write x B for ¬ (x B), where is written using \notin. For the other set constructions, the defining equivalences in the last chapter hold definitionally. The following example illustrates these features.

example :  x, x  A  x  B  x  A  B :=
assume x,
assume : x  A,
assume : x  B,
show x  A  B, from and.intro x  A x  B

example : A  A  B :=
assume x,
assume : x  A,
show x  A  B, from or.inl this

example :   A  :=
assume x,
assume : x  ,
show x  A, from false.elim x  ( : set U)›
example :  x, x  A  x  B  x  A  B :=
assume x,
assume : x  A,
assume : x  B,
show x  A  B, from and.intro x  A x  B

example : A  A  B :=
assume x,
assume : x  A,
show x  A  B, from or.inl this

example :   A  :=
assume x,
assume : x  ,
show x  A, from false.elim x  ( : set U)›

记住第 4.5 节中的内容,我们可以使用不带标签的 ,并使用法语引号引用假设,以和assume开头。我们在上一个示例中使用了此功能。如果没有该功能,我们可以将上述示例编写如下:\f<\f>

Remember from Section 4.5 that we can use assume without a label, and refer back to hypotheses using French quotes, entered with \f< and \f>. We have used this feature in the previous example. Without that feature, we could have written the examples above as follows:

example :  x, x  A  x  B  x  A  B :=
assume x,
assume h1 : x  A,
assume h2 : x  B,
show x  A  B, from and.intro h1 h2

example : A  A  B :=
assume x,
assume h : x  A,
show x  A  B, from or.inl h

example :   A  :=
assume x,
assume h : x  ( : set U),
show x  A, from false.elim h
example :  x, x  A  x  B  x  A  B :=
assume x,
assume h1 : x  A,
assume h2 : x  B,
show x  A  B, from and.intro h1 h2

example : A  A  B :=
assume x,
assume h : x  A,
show x  A  B, from or.inl h

example :   A  :=
assume x,
assume h : x  ( : set U),
show x  A, from false.elim h

下面,以及在随后的章节中,我们将开始使用不带标签assumehave命令,但您可以自由采用您喜欢的任何风格。

Below, and in the chapters that follow, we will begin to use assume and have command without labels, but you should feel free to adopt whatever style you prefer.

还要注意,在上一个例子中,我们必须通过书写来注释空集,以告诉 Lean 我们指的是哪个空集。Lean 通常可以从上下文中推断出这样的信息(例如,从我们试图展示的事实来看,其中类型为),但在这种情况下,它需要更多的帮助。(∅ : set U)x AAset U

Notice also that in the last example, we had to annotate the empty set by writing (∅ : set U) to tell Lean which empty set we mean. Lean can often infer information like this from the context (for example, from the fact that we are trying to show x A, where A has type set U), but in this case, it needs a bit more help.

或者,我们可以使用 Lean 库中专为集合设计的定理:

Alternatively, we can use theorems in the Lean library that are designed specifically for use with sets:

example :  x, x  A  x  B  x  A  B :=
assume x,
assume : x  A,
assume : x  B,
show x  A  B, from mem_inter x  A x  B

example : A  A  B :=
assume x,
assume h : x  A,
show x  A  B, from mem_union_left B h

example :   A  :=
assume x,
assume : x  ,
show x  A, from absurd this (not_mem_empty x)
example :  x, x  A  x  B  x  A  B :=
assume x,
assume : x  A,
assume : x  B,
show x  A  B, from mem_inter x  A x  B

example : A  A  B :=
assume x,
assume h : x  A,
show x  A  B, from mem_union_left B h

example :   A  :=
assume x,
assume : x  ,
show x  A, from absurd this (not_mem_empty x)

请记住,absurd可用于从两个相互矛盾的假设和证明任何事实。 这里是事实。 您可以使用Lean 中的命令查看定理的陈述:h1 : Ph2 : ¬ Pnot_mem_empty xx #check

Remember that absurd can be used to prove any fact from two contradictory hypotheses h1 : P and h2 : ¬ P. Here the not_mem_empty x is the fact x . You can see the statements of the theorems using the #check command in Lean:

#check @mem_inter
#check @mem_of_mem_inter_left
#check @mem_of_mem_inter_right
#check @mem_union_left
#check @mem_union_right
#check @mem_or_mem_of_mem_union
#check @not_mem_empty
#check @mem_inter
#check @mem_of_mem_inter_left
#check @mem_of_mem_inter_right
#check @mem_union_left
#check @mem_union_right
#check @mem_or_mem_of_mem_union
#check @not_mem_empty

这里,@Lean 中的符号阻止它尝试自动填充隐式参数,迫使它显示定理的完整陈述。

Here, the @ symbol in Lean prevents it from trying to fill in implicit arguments automatically, forcing it to display the full statement of the theorem.

Lean 可以通过逻辑定义来识别集合,这使得证明集合之间的包含关系变得容易:

The fact that Lean can identify sets with their logical definitions makes it easy to prove inclusions between sets:

example : A \ B  A :=
assume x,
assume : x  A \ B,
show x  A, from and.left this

example : A \ B  B :=
assume x,
assume : x  A \ B,
have x  B, from and.right this,
show x  B, from this
example : A \ B  A :=
assume x,
assume : x  A \ B,
show x  A, from and.left this

example : A \ B  B :=
assume x,
assume : x  A \ B,
have x  B, from and.right this,
show x  B, from this

再次,我们可以使用专门为集合设计的定理:

Once again, we can use the theorems designed specifically for sets:

example : A \ B  A :=
assume x,
assume : x  A \ B,
show x  A, from mem_of_mem_diff this

example : A \ B  B :=
assume x,
assume : x  A \ B,
have x  B, from not_mem_of_mem_diff this,
show x  B, from this
example : A \ B  A :=
assume x,
assume : x  A \ B,
show x  A, from mem_of_mem_diff this

example : A \ B  B :=
assume x,
assume : x  A \ B,
have x  B, from not_mem_of_mem_diff this,
show x  B, from this

Lean 必须展开定义,这意味着它有时会令人困惑。例如,在下面的证明中,如果你将最后一行替换为sorry,Lean 很难弄清楚你希望它展开子集符号:

The fact that Lean has to unfold definitions means that it can be confused at times. For example, in the proof below, if you replace the last line by sorry, Lean has trouble figuring out that you want it to unfold the subset symbol:

variable  {U : Type}
variables A B : set U

example : A  B  B  A :=
assume x,
assume h : x  A  B,
have h1 : x  A, from and.left h,
have h2 : x  B, from and.right h,
and.intro h2 h1
variable  {U : Type}
variables A B : set U

example : A  B  B  A :=
assume x,
assume h : x  A  B,
have h1 : x  A, from and.left h,
have h2 : x  B, from and.right h,
and.intro h2 h1

一种解决方法是使用show命令;一般来说,向 Lean 提供此类附加信息通常很有帮助。另一种解决方法是给定理命名,这会促使 Lean 使用略有不同的方法来处理证明,从而将问题作为幸运的副作用解决。

One workaround is to use the show command; in general, providing Lean with such additional information is often helpful. Another workaround is to give the theorem a name, which prompts Lean to use a slightly different method of processing the proof, fixing the problem as a lucky side effect.

example : A  B  B  A :=
assume x,
assume h : x  A  B,
have h1 : x  A, from and.left h,
have h2 : x  B, from and.right h,
show x  B  A, from sorry

theorem my_example : A  B  B  A :=
assume x,
assume h : x  A  B,
have h1 : x  A, from and.left h,
have h2 : x  B, from and.right h,
sorry
example : A  B  B  A :=
assume x,
assume h : x  A  B,
have h1 : x  A, from and.left h,
have h2 : x  B, from and.right h,
show x  B  A, from sorry

theorem my_example : A  B  B  A :=
assume x,
assume h : x  A  B,
have h1 : x  A, from and.left h,
have h2 : x  B, from and.right h,
sorry

12.2.一些身份

12.2. Some Identities

这是我们在上一章中非正式证明的第一个恒等式的证明:

Here is the proof of the first identity that we proved informally in the previous chapter:

example : A  (B  C) = (A  B)  (A  C) :=
eq_of_subset_of_subset
  (assume x,
    assume h : x  A  (B  C),
    have x  A, from and.left h,
    have x  B  C, from and.right h,
    or.elim (‹x  B  C›)
      (assume : x  B,
        have x  A  B, from and.intro x  A x  B›,
        show x  (A  B)  (A  C), from or.inl this)
      (assume : x  C,
        have x  A  C, from and.intro x  A x  C›,
        show x  (A  B)  (A  C), from or.inr this))
  (assume x,
    assume : x  (A  B)  (A  C),
    or.elim this
      (assume h : x  A  B,
        have x  A, from and.left h,
        have x  B, from and.right h,
        have x  B  C, from or.inl this,
        show x  A  (B  C), from and.intro x  A this)
      (assume h : x  A  C,
        have x  A, from and.left h,
        have x  C, from and.right h,
        have x  B  C, from or.inr this,
        show x  A  (B  C), from and.intro x  A this))
example : A  (B  C) = (A  B)  (A  C) :=
eq_of_subset_of_subset
  (assume x,
    assume h : x  A  (B  C),
    have x  A, from and.left h,
    have x  B  C, from and.right h,
    or.elim (‹x  B  C›)
      (assume : x  B,
        have x  A  B, from and.intro x  A x  B›,
        show x  (A  B)  (A  C), from or.inl this)
      (assume : x  C,
        have x  A  C, from and.intro x  A x  C›,
        show x  (A  B)  (A  C), from or.inr this))
  (assume x,
    assume : x  (A  B)  (A  C),
    or.elim this
      (assume h : x  A  B,
        have x  A, from and.left h,
        have x  B, from and.right h,
        have x  B  C, from or.inl this,
        show x  A  (B  C), from and.intro x  A this)
      (assume h : x  A  C,
        have x  A, from and.left h,
        have x  C, from and.right h,
        have x  B  C, from or.inr this,
        show x  A  (B  C), from and.intro x  A this))

请注意,它比上一章中的非正式证明长得多,因为我们已经详细说明了每一个细节。不幸的是,这并不一定会使其更具可读性。请记住,您可以随时使用逐步编写长证明sorry。您还可以将长证明分解成较小的部分:

Notice that it is considerably longer than the informal proof in the last chapter, because we have spelled out every last detail. Unfortunately, this does not necessarily make it more readable. Keep in mind that you can always write long proofs incrementally, using sorry. You can also break up long proofs into smaller pieces:

theorem inter_union_subset : A  (B  C)  (A  B)  (A  C) :=
assume x,
assume h : x  A  (B  C),
have x  A, from and.left h,
have x  B  C, from and.right h,
or.elim (‹x  B  C›)
  (assume : x  B,
    have x  A  B, from and.intro x  A x  B›,
    show x  (A  B)  (A  C), from or.inl this)
  (assume : x  C,
    have x  A  C, from and.intro x  A x  C›,
    show x  (A  B)  (A  C), from or.inr this)

theorem inter_union_inter_subset :
  (A  B)  (A  C)  A  (B  C) :=
assume x,
assume : x  (A  B)  (A  C),
or.elim this
  (assume h : x  A  B,
    have x  A, from and.left h,
    have x  B, from and.right h,
    have x  B  C, from or.inl this,
    show x  A  (B  C), from and.intro x  A this)
  (assume h : x  A  C,
    have x  A, from and.left h,
    have x  C, from and.right h,
    have x  B  C, from or.inr this,
    show x  A  (B  C), from and.intro x  A this)

example : A  (B  C) = (A  B)  (A  C) :=
eq_of_subset_of_subset
  (inter_union_subset A B C)
  (inter_union_inter_subset A B C)
theorem inter_union_subset : A  (B  C)  (A  B)  (A  C) :=
assume x,
assume h : x  A  (B  C),
have x  A, from and.left h,
have x  B  C, from and.right h,
or.elim (‹x  B  C›)
  (assume : x  B,
    have x  A  B, from and.intro x  A x  B›,
    show x  (A  B)  (A  C), from or.inl this)
  (assume : x  C,
    have x  A  C, from and.intro x  A x  C›,
    show x  (A  B)  (A  C), from or.inr this)

theorem inter_union_inter_subset :
  (A  B)  (A  C)  A  (B  C) :=
assume x,
assume : x  (A  B)  (A  C),
or.elim this
  (assume h : x  A  B,
    have x  A, from and.left h,
    have x  B, from and.right h,
    have x  B  C, from or.inl this,
    show x  A  (B  C), from and.intro x  A this)
  (assume h : x  A  C,
    have x  A, from and.left h,
    have x  C, from and.right h,
    have x  B  C, from or.inr this,
    show x  A  (B  C), from and.intro x  A this)

example : A  (B  C) = (A  B)  (A  C) :=
eq_of_subset_of_subset
  (inter_union_subset A B C)
  (inter_union_inter_subset A B C)

请注意,这两个定理依赖于变量ABC,在应用它们时必须将它们作为参数提供。它们还依赖于底层类型 ,U但由于变量U被标记为隐式,因此 Lean 会根据上下文来推断。

Notice that the two theorems depend on the variables A, B, and C, which have to be supplied as arguments when they are applied. They also depend on the underlying type, U, but because the variable U was marked implicit, Lean figures it out from the context.

在上一章中,我们展示了(AB)B=B。以下是Lean中对应的证明:

In the last chapter, we showed (AB)B=B. Here is the corresponding proof in Lean:

example : (A  B)  B = A  B :=
calc
  (A  B)  B = (A  B)  (B  B) : by rw union_distrib_right
           ... = (A  B)  univ     : by rw compl_union_self
           ... = A  B              : by rw inter_univ
example : (A  B)  B = A  B :=
calc
  (A  B)  B = (A  B)  (B  B) : by rw union_distrib_right
           ... = (A  B)  univ     : by rw compl_union_self
           ... = A  B              : by rw inter_univ

转化为命题,上述定理指出,对于每一对元素AB在布尔代数中,(A¬B)B=B。Lean 允许我们对命题进行计算,就好像它们是布尔代数的元素一样,用 代替等式

Translated to propositions, the theorem above states that for every pair of elements A and B in a Boolean algebra, (A¬B)B=B. Lean allows us to do calculations on propositions as though they are elements of a Boolean algebra, with equality replaced by .

variables A B : Prop

example : (A  ¬ B)  B  A  B :=
calc
  (A  ¬ B)  B  (A  B)  (¬ B  B) : by rw and_or_distrib_right
            ...  (A  B)  true      : by rw not_or_self
            ...  (A  B)             : by rw and_true
variables A B : Prop

example : (A  ¬ B)  B  A  B :=
calc
  (A  ¬ B)  B  (A  B)  (¬ B  B) : by rw and_or_distrib_right
            ...  (A  B)  true      : by rw not_or_self
            ...  (A  B)             : by rw and_true

12.3.索引家族

12.3. Indexed Families

请记住,如果(Ai)iI是一个集合族,索引为I, 然后iIAi表示所有集合的交集Ai, 和iIAi表示它们的并集。在 Lean 中,我们可以通过以下方式指定A是集合族,其中是。换句话说,集合族实际上是一个函数,它对类型的每个元素返回一个集合。然后我们可以定义并集和交集如下:A : I set UITypeiIA i

Remember that if (Ai)iI is a family of sets indexed by I, then iIAi denotes the intersection of all the sets Ai, and iIAi denotes their union. In Lean, we can specify that A is a family of sets by writing A : I set U where I is a Type. In other words, a family of sets is really a function which for each element i of type I returns a set A i. We can then define the union and intersection as follows:

variables {I U : Type}

def Union (A : I  set U) : set U := { x |  i : I, x  A i }
def Inter (A : I  set U) : set U := { x |  i : I, x  A i }

section
variables (x : U) (A : I  set U)

example (h : x  Union A) :  i, x  A i := h
example (h : x  Inter A) :  i, x  A i := h
end
variables {I U : Type}

def Union (A : I  set U) : set U := { x |  i : I, x  A i }
def Inter (A : I  set U) : set U := { x |  i : I, x  A i }

section
variables (x : U) (A : I  set U)

example (h : x  Union A) :  i, x  A i := h
example (h : x  Inter A) :  i, x  A i := h
end

这些示例表明,Lean 可以展开定义,以便可以视为,可以视为。要重温如何使用 Lean 中的通用量词和存在量词,请参阅第 9 章。然后我们可以定义索引并集和交集的符号:x Inter A i, x A ix Union A i, x A i

The examples show that Lean can unfold the definitions so that x Inter A can be treated as i, x A i and x Union A can be treated as i, x A i. To refresh your memory as to how to work with the universal and existential quantifiers in Lean, see Chapters 9. We can then define notation for the indexed union and intersection:

notation `⋃` binders `, ` r:(scoped f, Union f) := r
notation `⋂` binders `, ` r:(scoped f, Inter f) := r

variables (A : I  set U) (x : U)

example (h : x   i, A i) :  i, x  A i := h
example (h : x   i, A i) :  i, x  A i := h
notation `⋃` binders `, ` r:(scoped f, Union f) := r
notation `⋂` binders `, ` r:(scoped f, Inter f) := r

variables (A : I  set U) (x : U)

example (h : x   i, A i) :  i, x  A i := h
example (h : x   i, A i) :  i, x  A i := h

您可以分别将和键入\I\Un。与量词一样,符号和绑定表达式中的变量,并且范围尽可能广泛。例如,如果您写,Lean 会假定序列的第 i 个元素是。如果您想将范围限制得更窄,请使用括号。 i, A i i, A ii i, A i BA i B

You can type and with \I and \Un, respectively. As with quantifiers, the notation i, A i and i, A i bind the variable i in the expression, and the scope extends as widely as possible. For example, if you write i, A i B, Lean assumes that the ith element of the sequence is A i B. If you want to restrict the scope more narrowly, use parentheses.

好消息是,Lean 的库确实用这种符号定义了索引并集和交集,并且这些定义可以通过 获得。坏消息是,它使用了不同的定义,因此和在定义上不等于和如上所述。好消息是 Lean 至少知道它们是等价的:import data.setx Inter Ax Union A i, x A i i, x A i

The good news is that Lean’s library does define indexed union and intersection, with this notation, and the definitions are made available with import data.set. The bad news is that it uses a different definition, so that x Inter A and x Union A are not definitionally equal to i, x A i and i, x A i, as above. The good news is that Lean at least knows that they are equivalent:

import data.set
open set

variables {I U : Type}
variables {A B : I  set U}

theorem exists_of_mem_Union {x : U} (h : x   i, A i) :
   i, x  A i :=
by simp * at *

theorem mem_Union_of_exists {x : U} (h :  i, x  A i) :
  x   i, A i :=
by simp * at *

theorem forall_of_mem_Inter {x : U} (h : x   i, A i) :
   i, x  A i :=
by simp * at *

theorem mem_Inter_of_forall {x : U} (h :  i, x  A i) :
  x   i, A i :=
by simp * at *
import data.set
open set

variables {I U : Type}
variables {A B : I  set U}

theorem exists_of_mem_Union {x : U} (h : x   i, A i) :
   i, x  A i :=
by simp * at *

theorem mem_Union_of_exists {x : U} (h :  i, x  A i) :
  x   i, A i :=
by simp * at *

theorem forall_of_mem_Inter {x : U} (h : x   i, A i) :
   i, x  A i :=
by simp * at *

theorem mem_Inter_of_forall {x : U} (h :  i, x  A i) :
  x   i, A i :=
by simp * at *

该命令调用 Lean 的自动化程序来执行校样。以下是如何使用它们的一个示例:simp * at *

The command simp * at * calls upon Lean’s automation to carry out the proofs. Here is an example of how these can be used:

example : ( i, A i  B i) = ( i, A i)  ( i, B i) :=
ext $
assume x : U,
iff.intro
(assume h : x   i, A i  B i,
    have h1 :  i, x  A i  B i, from forall_of_mem_Inter h,
    have h2 :  i, x  A i, from assume i, and.left (h1 i),
    have h3 :  i, x  B i, from assume i, and.right (h1 i),
    have h4 : x   i, A i, from mem_Inter_of_forall h2,
    have h5 : x   i, B i, from mem_Inter_of_forall h3,
    and.intro h4 h5)
(assume h : x  ( i, A i)  ( i, B i),
    have h1 :  i, x  A i,
      from forall_of_mem_Inter (and.left h),
    have h2 :  i, x  B i,
      from forall_of_mem_Inter (and.right h),
    have h3 :  i, x  A i  B i,
      from assume i, and.intro (h1 i) (h2 i),
    show x   i, A i  B i, from mem_Inter_of_forall h3)
example : ( i, A i  B i) = ( i, A i)  ( i, B i) :=
ext $
assume x : U,
iff.intro
(assume h : x   i, A i  B i,
    have h1 :  i, x  A i  B i, from forall_of_mem_Inter h,
    have h2 :  i, x  A i, from assume i, and.left (h1 i),
    have h3 :  i, x  B i, from assume i, and.right (h1 i),
    have h4 : x   i, A i, from mem_Inter_of_forall h2,
    have h5 : x   i, B i, from mem_Inter_of_forall h3,
    and.intro h4 h5)
(assume h : x  ( i, A i)  ( i, B i),
    have h1 :  i, x  A i,
      from forall_of_mem_Inter (and.left h),
    have h2 :  i, x  B i,
      from forall_of_mem_Inter (and.right h),
    have h3 :  i, x  A i  B i,
      from assume i, and.intro (h1 i) (h2 i),
    show x   i, A i  B i, from mem_Inter_of_forall h3)

更好的是,我们可以证明交集和并集的引入和消除规则:

Even better, we can prove introduction and elimination rules for intersection and union:

import data.set
open set

variables {I U : Type}
variables {A : I  set U}

theorem Inter.intro {x : U} (h :  i, x  A i) : x   i, A i :=
by simp; assumption

@[elab_simple]
theorem Inter.elim {x : U} (h : x   i, A i) (i : I) : x  A i :=
by simp at h; apply h

theorem Union.intro {x : U} (i : I) (h : x  A i) :
  x   i, A i :=
by {simp, existsi i, exact h}

theorem Union.elim {b : Prop} {x : U}
(h₁ : x   i, A i) (h₂ :  (i : I), x  A i  b) : b :=
by {simp at h₁, cases h₁ with i h, exact h₂ i h}
import data.set
open set

variables {I U : Type}
variables {A : I  set U}

theorem Inter.intro {x : U} (h :  i, x  A i) : x   i, A i :=
by simp; assumption

@[elab_simple]
theorem Inter.elim {x : U} (h : x   i, A i) (i : I) : x  A i :=
by simp at h; apply h

theorem Union.intro {x : U} (i : I) (h : x  A i) :
  x   i, A i :=
by {simp, existsi i, exact h}

theorem Union.elim {b : Prop} {x : U}
(h₁ : x   i, A i) (h₂ :  (i : I), x  A i  b) : b :=
by {simp at h₁, cases h₁ with i h, exact h₂ i h}

不要担心证明的意义。重要的是如何使用它们,这就是以下模式所说明的:

Don’t worry about what the proofs mean. What is important is how they can be used, which is what the following patterns illustrate:

example (x : U) : x   i, A i :=
Inter.intro $
assume i,
show x  A i, from sorry

example (x : U) (i : I) (h : x   i, A i) : x  A i :=
Inter.elim h i

example (x : U) (i : I) (h : x  A i) : x   i, A i :=
Union.intro i h

example (C : Prop) (x : U) (h : x   i, A i) : C :=
Union.elim h $
assume i,
assume h : x  A i,
show C, from sorry
example (x : U) : x   i, A i :=
Inter.intro $
assume i,
show x  A i, from sorry

example (x : U) (i : I) (h : x   i, A i) : x  A i :=
Inter.elim h i

example (x : U) (i : I) (h : x  A i) : x   i, A i :=
Union.intro i h

example (C : Prop) (x : U) (h : x   i, A i) : C :=
Union.elim h $
assume i,
assume h : x  A i,
show C, from sorry

请记住,美元符号使我们免去了在其余证明周围加上括号的麻烦。请注意,对于Inter.introInter.elim,使用索引交集的证明看起来就像使用全称量词的证明一样。同样,Union.introUnion.elim反映了存在量词的引入和消除规则。以下示例提供了上面证明的等价关系的一个方向:

Remember that the dollar sign saves us the trouble of having to put parentheses around the rest of the proof. Notice that with Inter.intro and Inter.elim, proofs using indexed intersections looks just like proofs using the universal quantifier. Similarly, Union.intro and Union.elim mirror the introduction and elimination rules for the existential quantifier. The following example provides one direction of an equivalence proved above:

variables {I U : Type}
variables (A : I  set U) (B : I  set U) (C : set U)

example : ( i, A i  B i)  ( i, A i)  ( i, B i) :=
assume x : U,
assume h : x   i, A i  B i,
have h1 : x   i, A i, from
    Inter.intro $
    assume i : I,
    have h2 : x  A i  B i, from Inter.elim h i,
    show x  A i, from and.left h2,
have h2 : x   i, B i, from
    Inter.intro $
    assume i : I,
    have h2 : x  A i  B i, from Inter.elim h i,
    show x  B i, from and.right h2,
show x  ( i, A i)  ( i, B i), from and.intro h1 h2
variables {I U : Type}
variables (A : I  set U) (B : I  set U) (C : set U)

example : ( i, A i  B i)  ( i, A i)  ( i, B i) :=
assume x : U,
assume h : x   i, A i  B i,
have h1 : x   i, A i, from
    Inter.intro $
    assume i : I,
    have h2 : x  A i  B i, from Inter.elim h i,
    show x  A i, from and.left h2,
have h2 : x   i, B i, from
    Inter.intro $
    assume i : I,
    have h2 : x  A i  B i, from Inter.elim h i,
    show x  B i, from and.right h2,
show x  ( i, A i)  ( i, B i), from and.intro h1 h2

下面的练习要求您证明另一个方向。下面是一个示例,展示了如何使用索引并集的引入和消除规则:

You are asked to prove the other direction in the exercises below. Here is an example that shows how to use the introduction and elimination rules for indexed union:

variables {I U : Type}
variables (A : I  set U) (B : I  set U) (C : set U)

example : ( i, C  A i)  C  (i, A i) :=
assume x,
assume h : x   i, C  A i,
Union.elim h $
assume i,
assume h1 : x  C  A i,
have h2 : x  C, from and.left h1,
have h3 : x  A i, from and.right h1,
have h4 : x   i, A i, from Union.intro i h3,
show x  C   i, A i, from and.intro h2 h4
variables {I U : Type}
variables (A : I  set U) (B : I  set U) (C : set U)

example : ( i, C  A i)  C  (i, A i) :=
assume x,
assume h : x   i, C  A i,
Union.elim h $
assume i,
assume h1 : x  C  A i,
have h2 : x  C, from and.left h1,
have h3 : x  A i, from and.right h1,
have h4 : x   i, A i, from Union.intro i h3,
show x  C   i, A i, from and.intro h2 h4

再次,我们要求您在下面的练习中证明另一个方向。

Once again, we ask you to prove the other direction in the exercises below.

有时我们想与家人合作(Ai,j)iI,jJ由两个变量索引。这在 Lean 中也很容易管理:如果我们声明,那么给定和,我们有。(您应该将表达式解释为,因此具有类型,然后具有类型。)下面是一个涉及这种双索引系列的证明示例:A : I J set Ui : Ij : JA i j : set UI J set UI (J set U)A iJ set UA i jset U

Sometimes we want to work with families (Ai,j)iI,jJ indexed by two variables. This is also easy to manage in Lean: if we declare A : I J set U, then given i : I and j : J, we have that A i j : set U. (You should interpret the expression I J set U as I (J set U), so that A i has type J set U, and then A i j has type set U.) Here is an example of a proof involving a such a doubly-indexed family:

section
variables {I J U : Type}
variables (A : I  J  set U)

example : (i, j, A i j)  (j, i, A i j) :=
assume x,
assume h : x  i, j, A i j,
Union.elim h $
assume i,
assume h1 : x   j, A i j,
show x  j, i, A i j, from
    Inter.intro $
    assume j,
    have h2 : x  A i j, from Inter.elim h1 j,
    Union.intro i h2
end
section
variables {I J U : Type}
variables (A : I  J  set U)

example : (i, j, A i j)  (j, i, A i j) :=
assume x,
assume h : x  i, j, A i j,
Union.elim h $
assume i,
assume h1 : x   j, A i j,
show x  j, i, A i j, from
    Inter.intro $
    assume j,
    have h2 : x  A i j, from Inter.elim h1 j,
    Union.intro i h2
end

12.4.幂集

12.4. Power Sets

我们还可以在 Lean 中定义功率集:

We can also define the power set in Lean:

variable {U : Type}

def powerset (A : set U) : set (set U) := {B : set U | B  A}

example (A B : set U) (h : B  powerset A) : B  A :=
h
variable {U : Type}

def powerset (A : set U) : set (set U) := {B : set U | B  A}

example (A B : set U) (h : B  powerset A) : B  A :=
h

正如示例所示,在定义上与 相同。B powerset AB A

As the example shows, B powerset A is then definitionally the same as B A.

事实上,powerset在 Lean 中正是如此定义的,当你和时,你可以使用它。下面是如何使用它的一个例子:import data.setopen set

In fact, powerset is defined in Lean in exactly this way, and is available to you when you import data.set and open set. Here is an example of how it is used:

#check powerset A

example : A  powerset (A  B) :=
assume x,
assume : x  A,
show x  A  B, from or.inl x  A
#check powerset A

example : A  powerset (A  B) :=
assume x,
assume : x  A,
show x  A  B, from or.inl x  A

本质上,该示例证明了。在下面的练习中,我们要求您正式证明,对于每个,我们有A A BA B : set Upowerset A powerset B

In essence, the example proves A A B. In the exercises below, we ask you to prove, formally, that for every A B : set U, we have powerset A powerset B

12.5.练习

12.5. Exercises

  1. 填写sorry

    example :  x, x  A  C  x  A  B :=
    sorry
    
    example :  x, x  (A  B)  x  A :=
    sorry
    
  2. Fill in the sorry’s.

    example :  x, x  A  C  x  A  B :=
    sorry
    
    example :  x, x  (A  B)  x  A :=
    sorry
    
  3. 填写sorry

    import data.set
    open set
    
    section
    variable {U : Type}
    
    /- defining "disjoint" -/
    
    def disj (A B : set U) : Prop :=  x⦄, x  A  x  B  false
    
    example (A B : set U) (h :  x, ¬ (x  A  x  B)) :
      disj A B :=
    assume x,
    assume h1 : x  A,
    assume h2 : x  B,
    have h3 : x  A  x  B, from and.intro h1 h2,
    show false, from h x h3
    
    -- notice that we do not have to mention x when applying
    --   h : disj A B
    example (A B : set U) (h1 : disj A B) (x : U)
        (h2 : x  A) (h3 : x  B) :
      false :=
    h1 h2 h3
    
    -- the same is true of ⊆
    example (A B : set U) (x : U) (h : A  B) (h1 : x  A) :
      x  B :=
    h h1
    
    example (A B C D : set U) (h1 : disj A B) (h2 : C  A)
        (h3 : D  B) :
      disj C D :=
    sorry
    end
    
  4. Fill in the sorry.

    import data.set
    open set
    
    section
    variable {U : Type}
    
    /- defining "disjoint" -/
    
    def disj (A B : set U) : Prop :=  x⦄, x  A  x  B  false
    
    example (A B : set U) (h :  x, ¬ (x  A  x  B)) :
      disj A B :=
    assume x,
    assume h1 : x  A,
    assume h2 : x  B,
    have h3 : x  A  x  B, from and.intro h1 h2,
    show false, from h x h3
    
    -- notice that we do not have to mention x when applying
    --   h : disj A B
    example (A B : set U) (h1 : disj A B) (x : U)
        (h2 : x  A) (h3 : x  B) :
      false :=
    h1 h2 h3
    
    -- the same is true of ⊆
    example (A B : set U) (x : U) (h : A  B) (h1 : x  A) :
      x  B :=
    h h1
    
    example (A B C D : set U) (h1 : disj A B) (h2 : C  A)
        (h3 : D  B) :
      disj C D :=
    sorry
    end
    
  5. 利用上面列出的定理Inter.intro、、Inter.elimUnion.intro,证明有关索引并集和交集的以下事实。Union.elim

    variables {I U : Type}
    variables (A : I  set U) (B : I  set U) (C : set U)
    
    example : ( i, A i)  ( i, B i)  ( i, A i  B i) :=
    sorry
    
    example : C  (i, A i)  i, C  A i :=
    sorry
    
  6. Prove the following facts about indexed unions and intersections, using the theorems Inter.intro, Inter.elim, Union.intro, and Union.elim listed above.

    variables {I U : Type}
    variables (A : I  set U) (B : I  set U) (C : set U)
    
    example : ( i, A i)  ( i, B i)  ( i, A i  B i) :=
    sorry
    
    example : C  (i, A i)  i, C  A i :=
    sorry
    
  7. 证明以下关于幂集的事实。你可以使用定理subset.transsubset.refl

    variable  {U : Type}
    variables A B C : set U
    
    -- For this exercise these two facts are useful
    example (h1 : A  B) (h2 : B  C) : A  C :=
    subset.trans h1 h2
    
    example : A  A :=
    subset.refl A
    
    example (h : A  B) : powerset A  powerset B :=
    sorry
    
    example (h : powerset A  powerset B) : A  B :=
    sorry
    
  8. Prove the following fact about power sets. You can use the theorems subset.trans and subset.refl

    variable  {U : Type}
    variables A B C : set U
    
    -- For this exercise these two facts are useful
    example (h1 : A  B) (h2 : B  C) : A  C :=
    subset.trans h1 h2
    
    example : A  A :=
    subset.refl A
    
    example (h : A  B) : powerset A  powerset B :=
    sorry
    
    example (h : powerset A  powerset B) : A  B :=
    sorry
    

13.关系

13. Relations

在第 7 章中,我们讨论了一阶逻辑中的关系符号的概念,在第 10 章中,我们了解了如何在模型中解释这种符号。在数学中,我们通常对数学对象之间的不同类型的关系感兴趣,因此关系的概念无处不在。在本章中,我们将考虑一些常见的关系类型。

In Chapter 7 we discussed the notion of a relation symbol in first-order logic, and in Chapter 10 we saw how to interpret such a symbol in a model. In mathematics, we are generally interested in different sorts of relationships between mathematical objects, and so the notion of a relation is ubiquitous. In this chapter, we will consider some common kinds of relations.

在一些公理基础中,关系的概念被认为是原始的,但在公理集合论中,关系被认为是一组具有相应元数的元组。例如,我们可以取二元关系A是……的子集A×A, 在哪里R(a,b)意味着(a,b)R基础定义通常与日常数学实践无关;重要的是我们可以写出如下表达式R(a,b),并且它们是真还是假,取决于ab在数学中,我们经常使用中缀表示法,写作aRb而不是R(a,b)

In some axiomatic foundations, the notion of a relation is taken to be primitive, but in axiomatic set theory, a relation is taken to be a set of tuples of the corresponding arity. For example, we can take a binary relation on A to be a subset of A×A, where R(a,b) means that (a,b)R. The foundational definition is generally irrelevant to everyday mathematical practice; what is important is simply that we can write expressions like R(a,b), and that they are true or false, depending on the values of a and b. In mathematics, we often use infix notation, writing aRb instead of R(a,b).

13.1.顺序关系

13.1. Order Relations

我们将从数学中一类重要的二元关系开始,即偏序

We will start with a class of important binary relations in mathematics, namely, partial orders.


定义。二元关系在域上A如果具有以下三个属性,则为偏序:

Definition. A binary relation on a domain A is a partial order if it has the following three properties:

  • 反身性aa,对于每一个aA

  • reflexivity: aa, for every a in A

  • 传递性:如果abbc, 然后ac,对于每一个ab, 和cA

  • transitivity: if ab and bc, then ac, for every a, b, and c in A

  • 反对称性:若abba然后a=b,对于每一个abA

  • antisymmetry: if ab and ba then a=b, for every a and b in A


注意引入符号的紧凑方式在定义的陈述中,以及写成中缀符号。还要注意,即使关系用符号,它是定义中出现的唯一符号;数学实践倾向于使用自然语言来描述其属性。

Notice the compact way of introducing the symbol in the statement of the definition, and the fact that is written as an infix symbol. Notice also that even though the relation is written with the symbol , it is the only symbol occurring in the definition; mathematical practice favors natural language to describe its properties.

不过,现在您已经了解了足够的知识,可以识别这三个子句中存在的全称量词。在符号逻辑中,我们将其写成如下形式:

You now know enough, however, to recognize the universal quantifiers that are present in the three clauses. In symbolic logic, we would write them as follows:

  • a(aa)

  • a(aa)

  • a,b,c(abbcac)

  • a,b,c(abbcac)

  • a,b(abbaa=b)

  • a,b(abbaa=b)

这里的变量ab, 和c隐式地覆盖域A

Here the variables a, b, and c implicitly range over the domain A.

符号的使用旨在提供启发,事实上,以下都是偏序的例子:

The use of the symbol is meant to be suggestive, and, indeed, the following are all examples of partial orders:

  • 关于自然数

  • on the natural numbers

  • 关于整数

  • on the integers

  • 关于有理数

  • on the rational numbers

  • 实数

  • on the real numbers

但请记住只是一个符号,它也可以有令人意想不到的解释。例如,任何这些域上的关系也是偏序的,并且可以解释符号也一样。

But keep in mind that is only a symbol; it can have unexpected interpretations as well. For example, the relation on any of these domains is also a partial order, and can interpret the symbol just as well.

这些并不能完全代表偏序类,因为它们都具有一个附加属性:

These are not fully representative of the class of partial orders, in that they all have an additional property:


定义。偏序在域上A如果还具有以下属性,则为全序(也称为线性序):

Definition. A partial order on a domain A is a total order (also called a linear order) if it also has the following property:

  • 对于每一个abA, 任何一个ab或者ba

  • for every a and b in A, either ab or ba


您可以检查这些是部分顺序而不是全顺序的两个示例:

You can check these these are two examples of partial orders that are not total orders:

  • 分工关系,xy,整数

  • the divides relation, xy, on the integers

  • 子集关系,xy在某些域的元素集上A

  • the subset relation, xy, on sets of elements of some domain A

对于整数,我们也有严格的顺序关系,<,这不是偏序,因为它不是自反的。相反,它是严格偏序的一个实例:

On the integers, we also have the strict order relation, <, which is not a partial order, since it is not reflexive. It is, rather, an instance of a strict partial order:


定义。二元关系<在域上A如果满足以下条件,则为严格偏序:

Definition. A binary relation < on a domain A is a strict partial order if it satisfies the following:

  • 非反身性aa对于每一个aA

  • irreflexivity: aa for every a in A

  • 传递性a<bb<c暗示a<c,对于每一个ab, 和cA

  • transitivity: a<b and b<c implies a<c, for every a, b, and c in A

如果还具有以下属性,则严格偏序是严格全序(或严格线性序):

A strict partial order is a strict total order (or strict linear order) if, in addition, we have the following property:

  • 三分法a<ba=b, 或者a>b对于每一个abA

  • trichotomy: a<b, a=b, or a>b for every a and b in A


这里,ba当然,这意味着a<b, 和a>bb<a为了将普通偏序与严格偏序相区分,普通偏序有时也称为偏序。

Here, ba means, of course, that it is not the case that a<b, and a>b is alternative notation for b<a. To distinguish an ordinary partial order from a strict one, an ordinary partial order is sometimes called a weak partial order.


命题。严格偏序<A不对称的:对于每一个aba<b暗示ba

Proposition. A strict partial order < on A is asymmetric: for every a and b, a<b implies ba.

证明。假设a<bb<a。然后,根据传递性,a<a,与非反身性相矛盾。

Proof. Suppose a<b and b<a. Then, by transitivity, a<a, contradicting irreflexivity.


在整数上,<xy当且仅当x<y或者x=y, 和x<y当且仅当xyxy这说明了一个更加普遍的现象。

On the integers, there are precise relationships between < and : xy if and only if x<y or x=y, and x<y if and only if xy and xy. This illustrates a more general phenomenon.


定理。假设是域上的偏序A。 定义a<b意思是abab。 然后<是严格偏序。此外,如果是总计,所以是<

Theorem. Suppose is a partial order on a domain A. Define a<b to mean that ab and ab. Then < is a strict partial order. Moreover, if is total, so is <.

定理。假设<是域上的严格偏序A。 定义ab意思是a<b或者a=b。 然后是偏序的。此外,如果<是总计,所以是

Theorem. Suppose < is a strict partial order on a domain A. Define ab to mean a<b or a=b. Then is a partial order. Moreover, if < is total, so is .


我们将在这里证明第一个,将第二个留作练习。这个证明很好地说明了全称量化、等式和命题推理是如何在数学论证中结合起来的。

We will prove the first here, and leave the second as an exercise. This proof is a nice illustration of how universal quantification, equality, and propositional reasoning are combined in a mathematical argument.


证明。假设是偏序的A, 和<定义如下。非自反性是直接的,因为a<a暗示aa,这是一个矛盾。

Proof. Suppose is a partial order on A, and < be defined as in the statement of the theorem. Irreflexivity is immediate, since a<a implies aa, which is a contradiction.

为了证明传递性,假设a<bb<c.然后我们有abbcab, 和bc. 通过传递性,我们有ac. 显示a<c,我们只需展示ac.那么假设a=c然后,根据假设,我们有c<bb<c. 从定义<,我们有cbbc, 和cb.但前两个意味着c=b,矛盾。因此ac,按要求。

To show transitivity, suppose a<b and b<c. Then we have ab, bc, ab, and bc. By the transitivity of , we have ac. To show a<c, we only have to show ac. So suppose a=c. then, from the hypotheses, we have c<b and b<c. From the definition of <, we have cb, bc, and cb. But the first two imply c=b, a contradiction. So ac, as required.

为了建立定理中的最后一个断言,假设是总计,让ab是任何元素A我们需要证明a<ba=b, 或者a>b。 如果a=b,我们已经完成了,所以我们可以假设ab。 自从总计,我们有ab或者ba。 自从ab,在第一种情况下,我们有a<b,在第二种情况下,我们有a>b

To establish the last claim in the theorem, suppose is total, and let a and b be any elements of A. We need to show that a<b, a=b, or a>b. If a=b, we are done, so we can assume ab. Since is total, we have ab or ba. Since ab, in the first case we have a<b, and in the second case, we have a>b.


13.2.有关排序的更多信息

13.2. More on Orderings

是域上的偏序,A,并让<是相关的严格顺序,如上一节所定义。可以证明,如果我们朝另一个方向走,并定义与以下部分顺序相关<, 然后是相同的,也就是说,对于每一个abAab当且仅当ab。因此,我们可以认为每个偏序实际上都是一对,由一个弱偏序和一个相关的严格偏序组成。换句话说,我们可以假设x<y当且仅当xyxy,我们可以假设xy当且仅当x<y或者x=y

Let be a partial order on a domain, A, and let < be the associated strict order, as defined in the last section. It is possible to show that if we go in the other direction, and define to be the partial order associated to <, then and are the same, which is to say, for every a and b in A, ab if and only if ab. So we can think of every partial order as really being a pair, consisting of a weak partial order and an associated strict one. In other words, we can assume that x<y holds if and only if xy and xy, and we can assume xy holds if and only if x<y or x=y.

我们将从此采用这一惯例。给定一个部分顺序以及相关的严格秩序<,我们留给你来证明,如果xyy<z, 然后x<z,同样地,如果x<yyz, 然后x<z

We will henceforth adopt this convention. Given a partial order and the associated strict order <, we leave it to you to show that if xy and y<z, then x<z, and, similarly, if x<y and yz, then x<z.

考虑具有小于或等于关系的自然数。它有一个最小元素,0我们可以表达这样的事实0至少在两种情况下是最小元素:

Consider the natural numbers with the less-than-or-equal relation. It has a least element, 0. We can express the fact that 0 is the least element in at least two ways:

  • 0小于或等于每个自然数。

  • 0 is less than or equal to every natural number.

  • 没有小于的自然数0

  • There is no natural number that is less than 0.

在符号逻辑中,我们可以将这些语句形式化如下:

In symbolic logic, we could formalize these statements as follows:

  • x(0x)

  • x(0x)

  • x(x0)

  • x(x0)

使用存在量词,我们可以更忠实地呈现第二条语句,如下所示:

Using the existential quantifier, we could render the second statement more faithfully as follows:

  • ¬x(x<0)

  • ¬x(x<0)

请注意,这个更忠实的陈述与原始陈述等同,使用德摩根定律作为量词。

Notice that this more faithful statement is equivalent to the original, using deMorgan’s laws for quantifiers.

上述两个陈述是否等价?假设一个元素y如果它小于或等于任何其他元素,即如果它在第一个语句中取代了 0,那么它就是偏序的最小值。假设一个元素y对于偏序而言,如果没有元素小于它,即如果它在第二个语句中取代了 0,那么它就是最小的。有两个事实是显而易见的。

Are the two statements above equivalent? Say an element y is minimum for a partial order if it is less than or equal to any other element, that is, if it takes the place of 0 in the first statement. Say that an element y is minimal for a partial order if no element is less than it, that is, if it takes the place of 0 in the second statement. Two facts are immediate.


定理。任何最小元素都是最小的。

Theorem. Any minimum element is minimal.

证明。假设x是最小的我们需要证明x是最小的,也就是说对于每一个y,事实并非如此y<x。 认为y<x。 自从x是最小值,我们有xy。 从y<xxy,我们有y<y,与<

Proof. Suppose x is minimum for . We need to show that x is minimal, that is, for every y, it is not the case that y<x. Suppose y<x. Since x is minimum, we have xy. From y<x and xy, we have y<y, contradicting the irreflexivity of <.

定理。如果偏序有一个最小元素,它是唯一的。

Theorem. If a partial order has a minimum element, it is unique.

证明。假设x1x2都是最小值。那么x1x2x2x1.根据反对称性,x1=x2

Proof. Suppose x1 and x2 are both minimum. Then x1x2 and x2x1. By antisymmetry, x1=x2.


请注意,我们将第二定理解释为:如果x1x2都是最小值,那么x1=x2。事实上,这正是我们说某事物是“独一无二的”时的意思。当偏序具有最小元素时x,独特性是调用的理由x 最小元素。例如x也称为最小元素或最小元素,这两个术语通常可以互换。

Notice that we have interpreted the second theorem as the statement that if x1 and x2 are both minimum, then x1=x2. Indeed, this is exactly what we mean when we say that something is “unique.” When a partial order has a minimum element x, uniqueness is what justifies calling x the minimum element. Such an x is also called the least element or the smallest element, and the terms are generally interchangeable.

第一定理的逆定理(即每个最小元素都是最小的陈述)是错误的。例如,考虑集合的非空子集{1,2}子集关系。换句话说,考虑集合的集合{1}{2}, 和{1,2}, 在哪里{1}{1,2}{2}{1,2},当然,每个元素都是其自身的子集。然后你可以检查{1}{2}每一个都是最小的,但都不是最小的。(也可以通过画一个图来展示这种偏序,图中的点被标记为abc等,以及元素之间的向上边缘,以指示一个小于或等于另一个。)

The converse to the first theorem – that is, the statement that every minimal element is minimum – is false. As an example, consider the nonempty subsets of the set {1,2} with the subset relation. In other words, consider the collection of sets {1}, {2}, and {1,2}, where {1}{1,2}, {2}{1,2}, and, of course, every element is a subset of itself. Then you can check that {1} and {2} are each minimal, but neither is minimum. (One can also exhibit such a partial order by drawing a diagram, with dots labeled a, b, c, etc., and upwards edges between elements to indicate that one is less than or equal to the other.)

请注意,“偏序的最小元素不一定是最小的”这一陈述做出了一个“存在性”断言:它表示存在一个偏序以及一个元素x域,例如x是最小的但不是最小的。对于固定的偏序,我们可以断言,这样的x存在如下情况:

Notice that the statement “a minimal element of a partial order is not necessarily minimum” makes an “existential” assertion: it says that there is a partial order , and an element x of the domain, such that x is minimal but not minimum. For a fixed partial order , we can express the assertion that such an x exists as follows:

x(y(yx)¬y(xy)).

断言存在一个域A以及部分顺序在该域名上A,则更具戏剧性:这是一个“高阶”的存在性断言。但符号逻辑也为我们提供了做出此类断言的方法,我们稍后会看到。

The assertion that there exists a domain A, and a partial order on that domain A, is more dramatic: it is a “higher order” existential assertion. But symbolic logic provides us with the means to make assertions like these as well, as we will see later on.

我们可以考虑序的其他属性。如果任意两个不同元素之间有另一个元素,则称该序为稠密序。更准确地说,如果x<y,有一个元素z令人满意x<zz<y例如,有理数与通常的顺序,但不是整数。说一个顺序是稠密的,是隐含使用存在量化的另一个例子。

We can consider other properties of orders. An order is said to be dense if between any two distinct elements, there is another element. More precisely, an order is dense if, whenever x<y, there is an element z satisfying x<z and z<y. For example, the rational numbers are dense with the usual ordering, but not the integers. Saying that an order is dense is another example of an implicit use of existential quantification.

13.3.等价关系和相等性

13.3. Equivalence Relations and Equality

在普通的数学语言中,等价关系定义如下。

In ordinary mathematical language, an equivalence relation is defined as follows.


定义。二元关系在某些领域A如果一个关系是自反的、对称的、传递的,则称其为等价关系。换句话说,如果满足以下三个属性,则它是一种等价关系:

Definition. A binary relation on some domain A is said to be an equivalence relation if it is reflexive, symmetric, and transitive. In other words, is an equivalent relation if it satisfies these three properties:

  • 反身性aa,对于每一个aA

  • reflexivity: aa, for every a in A

  • 对称性:如果ab, 然后ba,对于每一个abA

  • symmetry: if ab, then ba, for every a and b in A

  • 传递性:如果abbc, 然后ac,对于每一个ab, 和cA

  • transitivity: if ab and bc, then ac, for every a, b, and c in A


我们让你思考如何用一阶逻辑编写这些语句。(请注意与偏序规则的相似性。)我们还将给你留下一个练习:通过仔细选择如何实例化量词,你实际上可以从以下两个属性中证明上述三个属性:

We leave it to you to think about how you could write these statements in first-order logic. (Note the similarity to the rules for a partial order.) We will also leave you with an exercise: by a careful choice of how to instantiate the quantifiers, you can actually prove the three properties above from the following two:

  • a(aa)

  • a(aa)

  • a,b,c(abcbac)

  • a,b,c(abcbac)

尝试使用自然演绎或精益来验证这一点。

Try to verify this using natural deduction or Lean.

仅凭这三个属性还不足以表征相等性。您应该检查以下非正式示例是否都是等价关系的实例:

These three properties alone are not strong enough to characterize equality. You should check that the following informal examples are all instances of equivalence relations:

  • 日历上的日子关系为“xy都落在一周的同一天”

  • the relation on days on the calendar, given by “x and y fall on the same day of the week”

  • 目前地球上活着的人们之间的关系,由“xy年龄相同”

  • the relation on people currently alive on the planet, given by “x and y have the same age”

  • 目前地球上活着的人们之间的关系,由“xy生日相同”

  • the relation on people currently alive on the planet, given by “x and y have the same birthday”

  • 美国城市的关系为“xy处于同一状态”

  • the relation on cities in the United States, given by “x and y are in the same state”

以下是两个常见的数学例子:

Here are two common mathematical examples:

  • 平面上直线的关系,由“xy是平行的”

  • the relation on lines in a plane, given by “x and y are parallel”

  • 对于任何固定的自然数m0,自然数上的关系,由“x符合于y模数m”(见第19章

  • for any fixed natural number m0, the relation on natural numbers, given by “x is congruent to y modulo m” (see Chapter 19)

在这里,我们说x符合于y模数m如果除以m很快,你就能严格证明,这相当于说xy可以被整除m

Here, we say that x is congruent to y modulo m if they leave the same remainder when divided by m. Soon, you will be able to prove rigorously that this is equivalent to saying that xy is divisible by m.

考虑美国公民的等价关系,如下“xy年龄相同。”有些属性尊重这种等价性。例如,假设我告诉你约翰和苏珊年龄相同,还告诉你约翰的年龄足以投票。那么你就可以正确地推断出苏珊的年龄足以投票。另一方面,如果我只告诉你约翰和苏珊年龄相同并且约翰住在南达科他州,你就不能推断出苏珊住在南达科他州。这个小例子说明了相等关系的特殊之处如果两件事相等,那么它们就有完全相同的属性。

Consider the equivalence relation on citizens of the United States, given by “x and y have the same age.” There are some properties that respect that equivalence. For example, suppose I tell you that John and Susan have the same age, and I also tell you that John is old enough to vote. Then you can rightly infer that Susan is old enough to vote. On the other hand, if I tell you nothing more than the facts that John and Susan have the same age and John lives in South Dakota, you cannot infer that Susan lives in South Dakota. This little example illustrates what is special about the equality relation: if two things are equal, then they have exactly the same properties.

A成为一个集合并让是等价关系A有一个重要的数学结构,称为形成A在等价关系下。对于每个元素aA, 让[a]是元素的集合{cca},即元素集A相当于a.我们称[a]等价类A. 该套装A/的商 A 经过 ,定义为集合{[a]:aA},即A下面的练习将向您展示如果[a][b]是该商的元素,则[a]=[b]当且仅当ab

Let A be a set and let be an equivalence relation on A. There is an important mathematical construction known as forming the quotient of A under the equivalence relation. For every element a in A, let [a] be the set of elements {cca}, that is, the set of elements of A that are equivalent to a. We call [a] the equivalence class of A. The set A/, the quotient of A by , is defined to be the set {[a]:aA}, that is, the set of all the equivalence classes of elements in A. The exercises below as you to show that if [a] and [b] are elements of such a quotient, then [a]=[b] if and only if ab.

动机如下。等价试图捕捉一个弱的平等概念:如果两个元素A是等价的,它们不一定相同,但在某些方面是相似的。等价类将相似的对象收集在一起,本质上是将它们合并成新的对象。因此A/是该集合的一个版本A其中相似的元素被压缩为单个元素。例如,给定等价关系整数模 5 一致性,N/是集合{[0],[1],[2],[3],[4]}例如,[0]是所有5的倍数的集合。

The motivation is as follows. Equivalence tries to capture a weak notion of equality: if two elements of A are equivalent, they are not necessarily the same, but they are similar in some way. Equivalence classes collect similar objects together, essentially glomming them into new objects. Thus A/ is a version of the set A where similar elements have been compressed into a single element. For example, given the equivalence relation of congruence modulo 5 on the integers, N/ is the set {[0],[1],[2],[3],[4]}, where, for example, [0] is the set of all multiples of 5.

13.4.练习

13.4. Exercises

  1. 认为<是域上的严格偏序A,并定义ab意思是a<b或者a=b

    • 表明是偏序的。

    • 表明如果<而且是严格全序,那么是一个全序。

    (上面我们证明了从另一个方向看类似的定理。)

  2. Suppose < is a strict partial order on a domain A, and define ab to mean that a<b or a=b.

    • Show that is a partial order.

    • Show that if < is moreover a strict total order, then is a total order.

    (Above we proved the analogous theorem going in the other direction.)

  3. 认为<是域上的严格偏序A。(换句话说,它是传递的和不对称的。)假设定义为ab当且仅当a<b或者a=b.我们在课堂上看到是域上的偏序A,即它是自反的、传递的、反对称的。

    证明对于每一个abA,我们有a<b当且仅当abab,使用上述事实。

  4. Suppose < is a strict partial order on a domain A. (In other words, it is transitive and asymmetric.) Suppose that is defined so that ab if and only if a<b or a=b. We saw in class that is a partial order on a domain A, i.e.~it is reflexive, transitive, and antisymmetric.

    Prove that for every a and b in A, we have a<b iff ab and ab, using the facts above.

  5. 有序图是顶点(点)的集合,以及顶点之间的箭头集合。对于每对顶点,它们之间最多有一个箭头:换句话说,每对顶点要么不相连,要么一个顶点“指向”另一个顶点。请注意,可以从一个顶点指向自身有一个箭头。

    定义关系在顶点集上,对于两个顶点abab表示有一支来自a指向b

    在任意图上,偏序、严格偏序、全序、严格全序,还是以上都不是?如果可能,请给出以下图表的例子:不具备这些属性。

  6. An ordered graph is a collection of vertices (points), along with a collection of arrows between vertices. For each pair of vertices, there is at most one arrow between them: in other words, every pair of vertices is either unconnected, or one vertex is “directed” toward the other. Note that it is possible to have an arrow from a vertex to itself.

    Define a relation on the set of vertices, such that for two vertices a and b, ab means that there is an arrow from a pointing to b.

    On an arbitrary graph, is a partial order, a strict partial order, a total order, a strict total order, or none of the above? If possible, give examples of graphs where fails to have these properties.

  7. 是集合上的等价关系A. 对于每个元素aA, 让[a]是等价类a:即元素集合{cca}. 证明对于每一个ab[a]=[b]当且仅当ab

    (提示和说明:

    • 请记住,由于您正在证明“当且仅当”语句,因此有两个方向需要证明。

    • 自此以后[a][b]是集合,[a]=[b]意味着对于每个元素cc位于[a]当且仅当c位于[b]

    • 根据定义,元素c位于[a]当且仅当ca。 尤其,a位于[a]

  8. Let be an equivalence relation on a set A. For every element a in A, let [a] be the equivalence class of a: that is, the set of elements {cca}. Show that for every a and b, [a]=[b] if and only if ab.

    (Hints and notes:

    • Remember that since you are proving an ``if and only if’’ statement, there are two directions to prove.

    • Since that [a] and [b] are sets, [a]=[b] means that for every element c, c is in [a] if and only if c is in [b].

    • By definition, an element c is in [a] if and only if ca. In particular, a is in [a].)

  9. 让关系关于自然数N定义如下:如果n是偶数,那么nn+1,如果n是奇数,那么nn1。此外,对于每一个nnn. 表明是等价关系。数字 5 的等价类是什么?描述等价类的集合{[n]nN}

  10. Let the relation on the natural numbers N be defined as follows: if n is even, then nn+1, and if n is odd, then nn1. Furthermore, for every n, nn. Show that is an equivalence relation. What is the equivalence class of the number 5? Describe the set of equivalence classes {[n]nN}.

  11. 证明平面上直线的关系由“l1l2是平行的”,是等价关系。x 轴的等价类是什么?描述等价类的集合{[l]l is a line in the plane}

  12. Show that the relation on lines in the plane, given by “l1 and l2 are parallel,” is an equivalence relation. What is the equivalence class of the x-axis? Describe the set of equivalence classes {[l]l is a line in the plane}.

  13. 二元关系在域上A被称为预序,它是自反的和传递的。这比说它是偏序要弱;我们删除了关系不对称的要求。一个例子是地球上目前活着的人的排序,其定义为xy当且仅当x出生日期早于y's。不对称性不成立,因为不同的人可以在同一天出生。但是,证明以下定理成立:

    定理。成为域名预订单A. 定义关系, 在哪里xy当且仅当xyyx。 然后是上的等价关系A

  14. A binary relation on a domain A is said to be a preorder it is is reflexive and transitive. This is weaker than saying it is a partial order; we have removed the requirement that the relation is asymmetric. An example is the ordering on people currently alive on the planet defined by setting xy if and only if x ‘s birth date is earlier than y ‘s. Asymmetry fails, because different people can be born on the same day. But, prove that the following theorem holds:

    Theorem. Let be a preorder on a domain A. Define the relation , where xy holds if and only if xy and yx. Then is an equivalence relation on A.

14.精益中的关系

14. Relations in Lean

在上一章中,我们注意到集合论者认为二元关系R在一组A作为一组有序对,因此R(a,b)确实意味着(a,b)R. 另一种方法是考虑R作为一个函数,当应用于ab,返回命题R(a,b)成立。这是 Lean 所采用的观点:类型上的二元关系A是函数。请记住,箭头向右关联,因此实际上意味着。因此,给定是一个谓词(与 相关的属性) ,而给定是一个命题。A A PropA A PropA (A Prop)a : AR aAa b : AR a b

In the last chapter, we noted that set theorists think of a binary relation R on a set A as a set of ordered pairs, so that R(a,b) really means (a,b)R. An alternative is to think of R as a function which, when applied to a and b, returns the proposition that R(a,b) holds. This is the viewpoint adopted by Lean: a binary relation on a type A is a function A A Prop. Remember that the arrows associate to the right, so A A Prop really means A (A Prop). So, given a : A, R a is a predicate (the property of being related to A), and given a b : A, R a b is a proposition.

14.1.顺序关系

14.1. Order Relations

利用一阶逻辑,我们可以说出关系的自反性、对称性、传递性、反对称性等等的含义:

With first-order logic, we can say what it means for a relation to be reflexive, symmetric, transitive, antisymmetric, and so on:

namespace hidden

variable {A : Type}

def reflexive (R : A  A  Prop) : Prop :=
 x, R x x

def symmetric (R : A  A  Prop) : Prop :=
 x y, R x y  R y x

def transitive (R : A  A  Prop) : Prop :=
 x y z, R x y  R y z  R x z

def anti_symmetric (R : A  A  Prop) : Prop :=
 x y, R x y  R y x  x = y

end hidden
namespace hidden

variable {A : Type}

def reflexive (R : A  A  Prop) : Prop :=
 x, R x x

def symmetric (R : A  A  Prop) : Prop :=
 x y, R x y  R y x

def transitive (R : A  A  Prop) : Prop :=
 x y z, R x y  R y z  R x z

def anti_symmetric (R : A  A  Prop) : Prop :=
 x y, R x y  R y x  x = y

end hidden

然后我们就可以自由使用这些概念了。请注意,精益会在必要时展开定义,例如,将视为。reflexive R x, R x x

We can then use the notions freely. Notice that Lean will unfold the definitions when necessary, for example, treating reflexive R as x, R x x.

variable R : A  A  Prop

example (h : reflexive R) (x : A) : R x x := h x

example (h : symmetric R) (x y : A) (h1 : R x y) : R y x :=
h x y h1

example (h : transitive R) (x y z : A) (h1 : R x y) (h2 : R y z) :
  R x z :=
h x y z h1 h2

example (h : anti_symmetric R) (x y : A) (h1 : R x y)
    (h2 : R y x) :
  x = y :=
h x y h1 h2
variable R : A  A  Prop

example (h : reflexive R) (x : A) : R x x := h x

example (h : symmetric R) (x y : A) (h1 : R x y) : R y x :=
h x y h1

example (h : transitive R) (x y z : A) (h1 : R x y) (h2 : R y z) :
  R x z :=
h x y z h1 h2

example (h : anti_symmetric R) (x y : A) (h1 : R x y)
    (h2 : R y x) :
  x = y :=
h x y h1 h2

在命令 中,我们用花括号括起来,表示它是一个隐式参数,也就是说,您不必明确地写出它;Lean 可以从参数 中推断出它。这就是为什么我们可以写而不是:Lean 知道是 上的二元关系,所以它可以推断出我们指的是 上的二元关系的反身性。variable {A : Type}ARreflexive Rreflexive A RRAA

In the command variable {A : Type}, we put curly braces around A to indicate that it is an implicit argument, which is to say, you do not have to write it explicitly; Lean can infer it from the argument R. That is why we can write reflexive R rather than reflexive A R: Lean knows that R is a binary relation on A, so it can infer that we mean reflexivity for binary relations on A.

给定、和,写出来证明很烦人。毕竟,Lean 应该能够从 和 的事实推断出我们在 、 和 处讨论的是传递性。事实上,我们可以用下划线代替该信息:h : transitive Rh1 : R x yh2 : R y zh x y z h1 h2R x zxyzh1R x yh2R y z

Given h : transitive R, h1 : R x y, and h2 : R y z, it is annoying to have to write h x y z h1 h2 to prove R x z. After all, Lean should be able to infer that we are talking about transitivity at x, y, and z, from the fact that h1 is R x y and h2 is R y z. Indeed, we can replace that information by underscores:

variable R : A  A  Prop

example (h : transitive R) (x y z : A) (h1 : R x y)
    (h2 : R y z) :
  R x z :=
h _ _ _ h1 h2
variable R : A  A  Prop

example (h : transitive R) (x y z : A) (h1 : R x y)
    (h2 : R y z) :
  R x z :=
h _ _ _ h1 h2

但输入下划线也很烦人。最好的解决方案是将传递性假设的参数也声明为隐式的:x y z

But typing underscores is annoying, too. The best solution is to declare the arguments x y z to a transitivity hypothesis to be implicit as well:

variable {A : Type}

variable R : A  A  Prop

example (h : transitive R) (x y z : A) (h1 : R x y) (h2 : R y z) :
  R x z :=
h h1 h2
variable {A : Type}

variable R : A  A  Prop

example (h : transitive R) (x y z : A) (h1 : R x y) (h2 : R y z) :
  R x z :=
h h1 h2

事实上, Lean 核心库中的概念reflexivesymmetrictransitive和正是以这种方式定义的,因此我们可以自由使用它们而无需定义它们。这就是为什么我们将 的临时定义放在命名空间 中的原因;这意味着我们版本的 的全名是,因此,它与库中定义的名称不冲突。anti_symmetrichiddenreflexivehidden.reflexive

In fact, the notions reflexive, symmetric, transitive, and anti_symmetric are defined in Lean’s core library in exactly this way, so we are free to use them without defining them. That is why we put our temporary definitions of in a namespace hidden; that means that the full name of our version of reflexive is hidden.reflexive, which, therefore, doesn’t conflict with the one defined in the library.

在第 13.1 节中,我们展示了严格偏序(即传递和非自反的二元关系)也是不对称的。以下是 Lean 中对该事实的证明。

In Section 13.1 we showed that a strict partial order—that is, a binary relation that is transitive and irreflexive—is also asymmetric. Here is a proof of that fact in Lean.

variable A : Type
variable R : A  A  Prop

example (h1 : irreflexive R) (h2 : transitive R) :
   x y, R x y  ¬ R y x :=
assume x y,
assume h3 : R x y,
assume h4 : R y x,
have h5 : R x x, from h2 h3 h4,
have h6 : ¬ R x x, from h1 x,
show false, from h6 h5
variable A : Type
variable R : A  A  Prop

example (h1 : irreflexive R) (h2 : transitive R) :
   x y, R x y  ¬ R y x :=
assume x y,
assume h3 : R x y,
assume h4 : R y x,
have h5 : R x x, from h2 h3 h4,
have h6 : ¬ R x x, from h1 x,
show false, from h6 h5

在数学中,通常使用中缀符号和符号来表示偏序。 Lean 支持这种做法:

In mathematics, it is common to use infix notation and a symbol like to denote a partial order. Lean supports this practice:

section
parameter A : Type
parameter R : A  A  Prop

local infix < := R

example (h1 : irreflexive R) (h2 : transitive R) :
   x y, x < y  ¬ y < x :=
assume x y,
assume h3 : x < y,
assume h4 : y < x,
have h5 : x < x, from h2 h3 h4,
have h6 : ¬ x < x, from h1 x,
show false, from h6 h5

end
section
parameter A : Type
parameter R : A  A  Prop

local infix < := R

example (h1 : irreflexive R) (h2 : transitive R) :
   x y, x < y  ¬ y < x :=
assume x y,
assume h3 : x < y,
assume h4 : y < x,
have h5 : x < x, from h2 h3 h4,
have h6 : ¬ x < x, from h1 x,
show false, from h6 h5

end

parameter和命令与和命令parameters类似,不同之处在于参数在节内是固定的。换句话说,如果您在上面的节中证明了关于 的定理,则您不能将该定理应用于另一个关系 ,除非关闭该节。由于参数是固定的,Lean 允许我们定义要在节中本地使用的 的符号。variablevariablesRSRR

The parameter and parameters commands are similar to the variable and variables commands, except that parameters are fixed within a section. In other words, if you prove a theorem about R in the section above, you cannot apply that theorem to another relation, S, without closing the section. Since the parameter R is fixed, Lean allows us to define notation for R to be used locally in the section.

在下面的例子中,固定了偏序,R我们定义相应的严格偏序,并证明它确实是一个严格序。

In the example below, having fixed a partial order, R, we define the corresponding strict partial order and prove that it is, indeed, a strict order.

section
parameters {A : Type} (R : A  A  Prop)
parameter (reflR : reflexive R)
parameter (transR : transitive R)
parameter (antisymmR :  {a b : A}, R a b  R b a  a = b)

local infix  := R

definition R' (a b : A) : Prop := a  b  a  b

local infix < := R'

theorem irreflR (a : A) : ¬ a < a :=
assume : a < a,
have a  a, from and.right this,
have a = a, from rfl,
show false, from a  a a = a

theorem transR {a b c : A} (h₁ : a < b) (h₂ : b < c) : a < c :=
have a  b, from and.left h₁,
have a  b, from and.right h₁,
have b  c, from and.left h₂,
have b  c, from and.right h₂,
have a  c, from transR a  b b  c›,
have a  c, from
    assume : a = c,
    have c  b, from eq.subst a = c a  b›,
    have b = c, from antisymmR b  c c  b›,
    show false, from b  c b = c›,
show a < c, from and.intro a  c a  c
end
section
parameters {A : Type} (R : A  A  Prop)
parameter (reflR : reflexive R)
parameter (transR : transitive R)
parameter (antisymmR :  {a b : A}, R a b  R b a  a = b)

local infix  := R

definition R' (a b : A) : Prop := a  b  a  b

local infix < := R'

theorem irreflR (a : A) : ¬ a < a :=
assume : a < a,
have a  a, from and.right this,
have a = a, from rfl,
show false, from a  a a = a

theorem transR {a b c : A} (h₁ : a < b) (h₂ : b < c) : a < c :=
have a  b, from and.left h₁,
have a  b, from and.right h₁,
have b  c, from and.left h₂,
have b  c, from and.right h₂,
have a  c, from transR a  b b  c›,
have a  c, from
    assume : a = c,
    have c  b, from eq.subst a = c a  b›,
    have b = c, from antisymmR b  c c  b›,
    show false, from b  c b = c›,
show a < c, from and.intro a  c a  c
end

请注意,我们使用了暗示性名称reflRtransRantisymmR而不是h1h2h3以帮助记住哪个假设是哪个假设。证明还使用了匿名have和,并使用法语引号anfassume引用它们。还要记住,这是事实的证明,相当于中的。您还可以使用等效符号,其中三角形写为。\f<\f>eq.subst ‹a = c› ‹a b›caa b‹a = c› ‹a b›\t

Notice that we have used suggestive names reflR, transR, antisymmR instead of h1, h2, h3 to help remember which hypothesis is which. The proof also uses anonymous have and assume, referring back to them with the French quotes, \f< anf \f>. Remember also that eq.subst ‹a = c› ‹a b› is a proof of the fact that amounts for substituting c for a in a b. You can also use the equivalent notation ‹a = c› ‹a b›, where the triangle is written \t.

在第 13.1节中,我们还指出,你可以从严格偏序中定义(弱)偏序。我们要求你在下面的练习中正式地做到这一点。

In Section Section 13.1, we also noted that you can define a (weak) partial order from a strict one. We ask you to do this formally in the exercises below.

这里还有一个例子。假设R是类型上的二元关系A,我们定义表示和都成立。下面我们表明结果关系是自反和对称的。S x yR x yR y x

Here is one more example. Suppose R is a binary relation on a type A, and we define S x y to mean that both R x y and R y x holds. Below we show that the resulting relation is reflexive and symmetric.

section
parameter A : Type
parameter R : A  A  Prop

variable h1 : transitive R
variable h2 : reflexive R

def S (x y : A) := R x y  R y x

example : reflexive S :=
assume x,
have R x x, from h2 x,
show S x x, from and.intro this this

example : symmetric S :=
assume x y,
assume h : S x y,
have h1 : R x y, from h.left,
have h2 : R y x, from h.right,
show S y x, from h.right, h.left

end
section
parameter A : Type
parameter R : A  A  Prop

variable h1 : transitive R
variable h2 : reflexive R

def S (x y : A) := R x y  R y x

example : reflexive S :=
assume x,
have R x x, from h2 x,
show S x x, from and.intro this this

example : symmetric S :=
assume x y,
assume h : S x y,
have h1 : R x y, from h.left,
have h2 : R y x, from h.right,
show S y x, from h.right, h.left

end

在下面的练习中,我们要求您证明这S也是及物的。

In the exercises below, we ask you to show that S is transitive as well.

在第一个例子中,我们使用匿名的assumeand have,然后使用have关键字引用回this。在第二个例子中,我们分别将and缩写为and 。我们还使用匿名构造函数缩写,写为。Lean 发现我们试图证明一个合取,并发现这是相关的引入原理。您可以分别用和键入角括号。and.left hand.right hh.lefth.rightand.intro h.right h.left⟨h.right, h.left⟩and.intro\<\>

In the first example, we use the anonymous assume and have, and then refer back to the have with the keyword this. In the second example, we abbreviate and.left h and and.right h as h.left and h.right, respectively. We also abbreviate and.intro h.right h.left with an anonymous constructor, writing ⟨h.right, h.left⟩. Lean figures out that we are trying to prove a conjunction, and figures out that and.intro is the relevant introduction principle. You can type the corner brackets with \< and \>, respectively.

14.2.数字排序

14.2. Orderings on Numbers

方便的是,Lean 已经定义了自然数、整数等的正常排序。

Conveniently, Lean has the normal orderings on the natural numbers, integers, and so on defined already.

open nat
variables n m : 

#check 0  n
#check n < n + 1

example : 0  n := nat.zero_le n
example : n < n + 1 := lt_succ_self n

example (h : n + 1  m) : n < m + 1 :=
have h1 : n < n + 1, from lt_succ_self n,
have h2 : n < m, from lt_of_lt_of_le h1 h,
have h3 : m < m + 1, from lt_succ_self m,
show n < m + 1, from lt_trans h2 h3
open nat
variables n m : 

#check 0  n
#check n < n + 1

example : 0  n := nat.zero_le n
example : n < n + 1 := lt_succ_self n

example (h : n + 1  m) : n < m + 1 :=
have h1 : n < n + 1, from lt_succ_self n,
have h2 : n < m, from lt_of_lt_of_le h1 h,
have h3 : m < m + 1, from lt_succ_self m,
show n < m + 1, from lt_trans h2 h3

Lean 中有许多定理可用于证明不等式关系的事实。我们在此列出一些常见的定理。

There are many theorems in Lean that are useful for proving facts about inequality relations. We list some common ones here.

variables (A : Type) [partial_order A]
variables a b c : A

#check (le_trans : a  b  b  c  a  c)
#check (lt_trans : a < b  b < c  a < c)
#check (lt_of_lt_of_le : a < b  b  c  a < c)
#check (lt_of_le_of_lt : a  b  b < c  a < c)
#check (le_of_lt : a < b  a  b)
variables (A : Type) [partial_order A]
variables a b c : A

#check (le_trans : a  b  b  c  a  c)
#check (lt_trans : a < b  b < c  a < c)
#check (lt_of_lt_of_le : a < b  b  c  a < c)
#check (lt_of_le_of_lt : a  b  b < c  a < c)
#check (le_of_lt : a < b  a  b)

这里顶部的声明表明它A具有偏序结构。还有一些特定于某些域的属性,例如自然数:

Here the declaration at the top says that A has the structure of a partial order. There are also properties that are specific to some domains, like the natural numbers:

variable n : 

#check (nat.zero_le :  n : , 0  n)
#check (nat.lt_succ_self :  n : , n < n + 1)
#check (nat.le_succ :  n : , n  n + 1)
variable n : 

#check (nat.zero_le :  n : , 0  n)
#check (nat.lt_succ_self :  n : , n < n + 1)
#check (nat.le_succ :  n : , n  n + 1)

14.3.等价关系

14.3. Equivalence Relations

13.3 节中,我们看到等价关系是某个域上的二元关系A即自反、对称和传递。稍后我们将在 Lean 中看到此类关系,但首先让我们定义另一种称为预序的关系,这是一种自反和传递的二元关系。

In Section 13.3 we saw that an equivalence relation is a binary relation on some domain A that is reflexive, symmetric, and transitive. We will see such relations in Lean in a moment, but first let’s define another kind of relation called a preorder, which is a binary relation that is reflexive and transitive.

namespace hidden

variable {A : Type}

def preorder (R : A  A  Prop) : Prop :=
reflexive R  transitive R

end hidden
namespace hidden

variable {A : Type}

def preorder (R : A  A  Prop) : Prop :=
reflexive R  transitive R

end hidden

Lean 的库提供了不同的预序公式,因此,为了使用相同的名称,我们必须将其放在hidden命名空间中。Lean 的库定义了关系的其他属性,例如:

Lean’s library provides a different formulation of preorders, so, in order to use the same name, we have to put it in the hidden namespace. Lean’s library defines other properties of relations, such as these:

namespace hidden

variables {A : Type} (R : A  A  Prop)

def equivalence := reflexive R  symmetric R  transitive R

def total :=  x y, R x y  R y x

def irreflexive :=  x, ¬ R x x

def anti_symmetric :=  x y⦄, R x y  R y x  x = y

end hidden
namespace hidden

variables {A : Type} (R : A  A  Prop)

def equivalence := reflexive R  symmetric R  transitive R

def total :=  x y, R x y  R y x

def irreflexive :=  x, ¬ R x x

def anti_symmetric :=  x y⦄, R x y  R y x  x = y

end hidden

你可以要求 Lean 打印它们的定义:

You can ask Lean to print their definitions:

#print equivalence
#print total
#print irreflexive
#print anti_symmetric
#print equivalence
#print total
#print irreflexive
#print anti_symmetric

基于我们之前对预序的定义,我们可以将偏序描述为反对称预序,并证明等价关系为对称预序。

Building on our previous definition of a preorder, we can describe a partial order as an antisymmetric preorder, and show that an equivalence relation as a symmetric preorder.

namespace hidden

variable {A : Type}

def preorder (R : A  A  Prop) : Prop :=
reflexive R  transitive R

def partial_order (R : A  A  Prop) : Prop :=
preorder R  anti_symmetric R

example (R : A  A  Prop):
  equivalence R  preorder R  symmetric R :=
iff.intro
  (assume h1 : equivalence R,
    have h2 : reflexive R, from and.left h1,
    have h3 : symmetric R, from and.left (and.right h1),
    have h4 : transitive R, from and.right (and.right h1),
    show preorder R  symmetric R,
      from and.intro (and.intro h2 h4) h3)
  (assume h1 : preorder R  symmetric R,
    have h2 : preorder R, from and.left h1,
    show equivalence R,
      from and.intro (and.left h2)
             (and.intro (and.right h1) (and.right h2)))

end hidden
namespace hidden

variable {A : Type}

def preorder (R : A  A  Prop) : Prop :=
reflexive R  transitive R

def partial_order (R : A  A  Prop) : Prop :=
preorder R  anti_symmetric R

example (R : A  A  Prop):
  equivalence R  preorder R  symmetric R :=
iff.intro
  (assume h1 : equivalence R,
    have h2 : reflexive R, from and.left h1,
    have h3 : symmetric R, from and.left (and.right h1),
    have h4 : transitive R, from and.right (and.right h1),
    show preorder R  symmetric R,
      from and.intro (and.intro h2 h4) h3)
  (assume h1 : preorder R  symmetric R,
    have h2 : preorder R, from and.left h1,
    show equivalence R,
      from and.intro (and.left h2)
             (and.intro (and.right h1) (and.right h2)))

end hidden

在第 13.3 节中,我们声称还有另一种定义等价关系的方法,即满足以下两个属性的二元关系:

In Section 13.3 we claimed that there is yet another way to define an equivalence relation, namely, as a binary relation satisfying the following two properties:

  • a(aa)

  • a(aa)

  • a,b,c(abcbac)

  • a,b,c(abcbac)

让我们在 Lean 中证明这一点。请记住,parameters和命令用于修复关系并引入符号来表示它。(您可以键入为。)在假设和中,符号表示。local infixR\~~reflexive (≈)symmetric (≈)(≈)R

Let’s prove this in Lean. Remember that the parameters and local infix commands serve to fix a relation R and introduce the notation to denote it. (You can type as \~~.) In the assumptions reflexive (≈) and symmetric (≈), the notation (≈) denotes R.

section
parameters {A : Type} (R : A  A  Prop)
local infix  := R

variable (h1 : reflexive ())
variable (h2 :  {a b c}, a  b  c  b  a  c)

example : symmetric () :=
assume a b (h : a  b),
have b  b  a  b, from and.intro (h1 b) h,
show b  a, from h2 this

example : transitive () :=
assume a b c (h3 : a  b) (h4 : b  c),
have c  b, from h2 (and.intro (h1 c) h4),
have a  b  c  b, from and.intro h3 this,
show a  c, from h2 this

end
section
parameters {A : Type} (R : A  A  Prop)
local infix  := R

variable (h1 : reflexive ())
variable (h2 :  {a b c}, a  b  c  b  a  c)

example : symmetric () :=
assume a b (h : a  b),
have b  b  a  b, from and.intro (h1 b) h,
show b  a, from h2 this

example : transitive () :=
assume a b c (h3 : a  b) (h4 : b  c),
have c  b, from h2 (and.intro (h1 c) h4),
have a  b  c  b, from and.intro h3 this,
show a  c, from h2 this

end

14.4.练习

14.4. Exercises

  1. 替换sorry以下证明中的命令,以表明我们可以R'​从严格偏序中创建偏序R

    section
    parameters {A : Type} {R : A  A  Prop}
    parameter (irreflR : irreflexive R)
    parameter (transR : transitive R)
    
    local infix < := R
    
    def R' (a b : A) : Prop := R a b  a = b
    local infix  := R'
    
    theorem reflR' (a : A) : a  a := sorry
    
    theorem transR' {a b c : A} (h1 : a  b) (h2 : b  c):
      a  c :=
    sorry
    
    theorem antisymmR' {a b : A} (h1 : a  b) (h2 : b  a) :
      a = b :=
    sorry
    
    end
    
  2. Replace the sorry commands in the following proofs to show that we can create a partial order R'​ out of a strict partial order R.

    section
    parameters {A : Type} {R : A  A  Prop}
    parameter (irreflR : irreflexive R)
    parameter (transR : transitive R)
    
    local infix < := R
    
    def R' (a b : A) : Prop := R a b  a = b
    local infix  := R'
    
    theorem reflR' (a : A) : a  a := sorry
    
    theorem transR' {a b c : A} (h1 : a  b) (h2 : b  c):
      a  c :=
    sorry
    
    theorem antisymmR' {a b : A} (h1 : a  b) (h2 : b  a) :
      a = b :=
    sorry
    
    end
    
  3. sorry用证明代替。

    section
    parameters {A : Type} {R : A  A  Prop}
    parameter (reflR : reflexive R)
    parameter (transR : transitive R)
    
    def S (a b : A) : Prop := R a b  R b a
    
    example : transitive S :=
    sorry
    
    end
    
  4. Replace the sorry by a proof.

    section
    parameters {A : Type} {R : A  A  Prop}
    parameter (reflR : reflexive R)
    parameter (transR : transitive R)
    
    def S (a b : A) : Prop := R a b  R b a
    
    example : transitive S :=
    sorry
    
    end
    
  5. 以下两个定理中只有一个是可证明的。找出哪一个是真的,并sorry用完整的证明替换命令。

    section
      parameters {A : Type} {a b c : A} {R : A  A  Prop}
      parameter (Rab : R a b)
      parameter (Rbc : R b c)
      parameter (nRac : ¬ R a c)
    
      -- Prove one of the following two theorems:
    
      theorem R_is_strict_partial_order :
        irreflexive R  transitive R :=
      sorry
    
      theorem R_is_not_strict_partial_order :
        ¬(irreflexive R  transitive R) :=
      sorry
    end
    
  6. Only one of the following two theorems is provable. Figure out which one is true, and replace the sorry command with a complete proof.

    section
      parameters {A : Type} {a b c : A} {R : A  A  Prop}
      parameter (Rab : R a b)
      parameter (Rbc : R b c)
      parameter (nRac : ¬ R a c)
    
      -- Prove one of the following two theorems:
    
      theorem R_is_strict_partial_order :
        irreflexive R  transitive R :=
      sorry
    
      theorem R_is_not_strict_partial_order :
        ¬(irreflexive R  transitive R) :=
      sorry
    end
    
  7. 完成下列证明。

    open nat
    
    example : 1  4 :=
    sorry
    
  8. Complete the following proof.

    open nat
    
    example : 1  4 :=
    sorry
    

15.函数

15. Functions

十九世纪末,数学许多分支学科的发展推动了集合、函数和关系的统一处理。我们已经讨论了集合和关系。在本章中,我们将讨论函数及其属性。

In the late nineteenth century, developments in a number of branches of mathematics pushed towards a uniform treatment of sets, functions, and relations. We have already considered sets and relations. In this chapter, we consider functions and their properties.

一个函数,f,通常被理解为来自域的映射X到另一个域Y在集合论基础中,XY是任意集合。我们已经看到,在像 Lean 这样的基于类型的系统中,区分类型和类型的子集是很自然的。换句话说,我们可以考虑一种X元素类型和一组A该类型的元素。因此,在类型理论公式中,考虑类型X和之间的函数是很自然的,并考虑它们相对于和Y的子集的行为。XY

A function, f, is ordinary understood as a mapping from a domain X to another domain Y. In set-theoretic foundations, X and Y are arbitrary sets. We have seen that in a type-based system like Lean, it is natural to distinguish between types and subsets of a type. In other words, we can consider a type X of elements, and a set A of elements of that type. Thus, in the type-theoretic formulation, it is natural to consider functions between types X and Y, and consider their behavior with respect to subsets of X and Y.

然而,在日常数学中,集合论语言很常见,大多数数学家认为函数是集合之间的映射。因此,当从数学角度讨论函数时,我们也将采用这种语言,稍后在讨论精益中的形式化时将切换到类型论表示。

In everyday mathematics, however, set-theoretic language is common, and most mathematicians think of a function as a map between sets. When discussing functions from a mathematical standpoint, therefore, we will also adopt this language, and later switch to the type-theoretic representation when we talk about formalization in Lean.

15.1.函数概念

15.1. The Function Concept

如果XY是任意集合,我们写为f:XY表达这一事实f是一个函数XY.这意味着f分配一个值f(x)Y对每一个元素xX. 该套装X称为​f,以及集合Y被称为陪域。(有些作者用“范围”这个词来表示陪域,但今天更常见的是使用“范围”这个词来表示我们所说A下面。我们将通过完全避免单词范围来避免歧义。

If X and Y are any sets, we write f:XY to express the fact that f is a function from X to Y. This means that f assigns a value f(x) in Y to every element x of X. The set X is called the domain of f, and the set Y is called the codomain. (Some authors use the word “range” for the codomain, but today it is more common to use the word “range” for what we call the image of A below. We will avoid the ambiguity by avoiding the word range altogether.)

定义函数最简单的方法是在每次x使用显式表达式。例如,我们可以编写以下任何一种:

The simplest way to define a function is to give its value at every x with an explicit expression. For example, we can write any of the following:

  • f:NN定义为函数f(n)=n+1

  • Let f:NN be the function defined by f(n)=n+1.

  • g:RR定义为函数g(x)=x2

  • Let g:RR be the function defined by g(x)=x2.

  • h:NN定义为函数h(n)=n2

  • Let h:NN be the function defined by h(n)=n2.

  • k:N{0,1}定义为函数

    k(n)={0if n is even1if n is odd.
  • Let k:N{0,1} be the function defined by

    k(n)={0if n is even1if n is odd.

使用显式表达式定义函数的能力提出了一个基本问题,即什么才算合法的“表达式”。现在,让我们把这个问题放在一边,只需注意现代数学对各种奇特的定义都很熟悉。例如,我们可以定义一个函数f:R{0,1}经过

The ability to define functions using an explicit expression raises the foundational question as to what counts as legitimate “expression.” For the moment, let us set that question aside, and simply note that modern mathematics is comfortable with all kinds of exotic definitions. For example, we can define a function f:R{0,1} by

f(x)={0if x is rational1if x is irrational.

这与函数在某种意义上是可计算对象的观点相矛盾。将实数作为输入意味着什么根本不清楚,更不用说是否有可能通过算法确定这样的数字是否合理。我们将在后面的章节中回顾这些问题。

This is at odds with a view of functions as objects that are computable in some sense. It is not at all clear what it means to be presented with a real number as input, let alone whether it is possible to determine, algorithmically, whether such a number is rational or not. We will return to such issues in a later chapter.

注意变量的选择xn上述定义中的变量是任意的。它们是绑定变量,因为定义的函数不依赖于x或者n。重命名后,值保持不变,就像“对于每个xP(x)”和“对于每一个yP(y)” 是相同的。给定一个表达式e(x)这取决于变量x逻辑学家经常使用符号λxe(x)表示映射函数xe(x)。这被称为“lambda 符号”,原因很明显,而且它通常非常方便。与其说“让f定义为函数f(x)=x+1”,我们可以说“让f=λx(x+1)”这不是常见的数学符号,除非你正在与逻辑学家或计算机科学家交谈,否则最好避免使用它。不过,我们将看到 lambda 符号内置于 Lean 中。

Notice that the choice of the variables x and n in the definitions above are arbitrary. They are bound variables in that the functions being defined do not depend on x or n. The values remain the same under renaming, just as the truth values of “for every x, P(x)” and “for every y, P(y)” are the same. Given an expression e(x) that depends on the variable x, logicians often use the notation λxe(x) to denote the function that maps x to e(x). This is called “lambda notation,” for the obvious reason, and it is often quite handy. Instead of saying “let f be the function defined by f(x)=x+1,” we can say “let f=λx(x+1).” This is not common mathematical notation, and it is best to avoid it unless you are talking to logicians or computer scientists. We will see, however, that lambda notation is built in to Lean.

对于任意集合X,我们可以定义一个函数iX(x)根据方程iX(x)=x。这个函数称为恒等函数。更有趣的是,让f:XYg:YZ我们可以定义一个新函数k:XZ经过k(x)=g(f(x)). 函数k称为 f g或者f 组成 g并且写道gf顺序有点混乱;你只需要记住,要评估表达式g(f(x))你首先评估f在输入上x,然后评估g

For any set X, we can define a function iX(x) by the equation iX(x)=x. This function is called the identity function. More interestingly, let f:XY and g:YZ. We can define a new function k:XZ by k(x)=g(f(x)). The function k is called the composition of f and g or f composed with g and it is written gf. The order is somewhat confusing; you just have to keep in mind that to evaluate the expression g(f(x)) you first evaluate f on input x, and then evaluate g.

我们想到两个函数f,g:XY当它们在每个输入上具有相同的值时,它们被视为相等或相同的函数;换句话说,对于每个xXf(x)=g(x)。例如,如果f,g:RR定义为f(x)=x+1g(x)=1+x, 然后f=g。请注意,两个函数相等的陈述是一个通用陈述(即,对于形式“对于每个x,……”)。

We think of two functions f,g:XY as being equal, or the same function, when for they have the same values on every input; in other words, for every x in X, f(x)=g(x). For example, if f,g:RR are defined by f(x)=x+1 and g(x)=1+x, then f=g. Notice that the statement that two functions are equal is a universal statement (that is, for the form “for every x, …”).


命题。对于每一个f:XYfiX=fiYf=f

Proposition. For every f:XY, fiX=f and iYf=f.

证明。x是任意元素X。 然后(fiX)(x)=f(iX(x))=f(x), 和(iYf)(x)=iY(f(x))=x

Proof. Let x be any element of X. Then (fiX)(x)=f(iX(x))=f(x), and (iYf)(x)=iY(f(x))=x.


认为f:XYg:YX满足gf=iX请记住,这意味着g(f(x))=x对于每一个xX。那么,g被称为f, 和f被称为g。以下是一些示例:

Suppose f:XY and g:YX satisfy gf=iX. Remember that this means that g(f(x))=x for every x in X. In that case, g is said to be a left inverse to f, and f is said to be a right inverse to g. Here are some examples:

  • 定义f,g:RR经过f(x)=x+1g(x)=x1。 然后gf,反之亦然。

  • Define f,g:RR by f(x)=x+1 and g(x)=x1. Then g is both a left and a right inverse to f, and vice-versa.

  • R0表示非负实数。定义f:RR0经过f(x)=x2,并定义g:R0R经过g(x)=x。 然后f(g(x))=(x)2=x对于每一个x在...的领域g, 所以fg, 和gf。 另一方面,g(f(x))=x2=|x|,这与x什么时候x为负。所以g不是左逆f, 和f不是右逆g

  • Write R0 to denote the nonnegative reals. Define f:RR0 by f(x)=x2, and define g:R0R by g(x)=x. Then f(g(x))=(x)2=x for every x in the domain of g, so f is a left inverse to g, and g is a right inverse to f. On the other hand, g(f(x))=x2=|x|, which is not the same as x when x is negative. So g is not a left inverse to f, and f is not a right inverse to g.

尽管证明很简短,但下列事实却并不明显:

The following fact is not at all obvious, even though the proof is short:


命题。假设f:XY有一个左逆,h以及右逆k。 然后h=k

Proposition. Suppose f:XY has a left inverse, h, and a right inverse, k. Then h=k.

证明。y是中的任意元素Y. 这个想法是计算h(f(k(y))两种不同的方式。由于hf,我们有h(f(k(y)))=k(y). 另一方面,由于kff(k(y))=y, 所以h(f(k(y))=h(y)。 所以k(y)=h(y)

Proof. Let y be any element in Y. The idea is to compute h(f(k(y)) in two different ways. Since h is a left inverse to f, we have h(f(k(y)))=k(y). On the other hand, since k is a right inverse to f, f(k(y))=y, and so h(f(k(y))=h(y). So k(y)=h(y).


如果gf,我们说g只是相反的f. 一个函数f可能有多个左逆或右逆(我们留给您自己去举例),但它最多可以有一个逆。

If g is both a right and left inverse to f, we say that g is simply the inverse of f. A function f may have more than one left or right inverse (we leave it to you to cook up examples), but it can have at most one inverse.


命题。假设g1,g2:YX都是逆的f。 然后g1=g2

Proposition. Suppose g1,g2:YX are both inverses to f. Then g1=g2.

证明。这可从前面的命题得出,因为(比如说)g1f, 和g2是右逆。

Proof. This follows from the previous proposition, since (say) g1 is a left inverse to f, and g2 is a right inverse.


什么时候f有一个逆,g,这证明调用g f和写作f1表示g. 请注意,如果f1f, 然后ff1. 因此如果f有逆,那么也有f1, 和(f1)1=f. 对于任何集合A,显然我们有iX1=iX

When f has an inverse, g, this justifies calling g the inverse to f, and writing f1 to denote g. Notice that if f1 is an inverse to f, then f is an inverse to f1. So if f has an inverse, then so does f1, and (f1)1=f. For any set A, clearly we have iX1=iX.


命题。假设f:XYg:YZ。 如果h:YXfk:ZYg, 然后hkgf

Proposition. Suppose f:XY and g:YZ. If h:YX is a left inverse to f and k:ZY is a left inverse to g, then hk is a left inverse to gf.

证明。对于每一个xX

Proof. For every x in X,

(hk)(gf)(x)=h(k(g(f(x))))=h(f(x))=x.

推论:将“左”替换为“右”,上一个命题仍然成立。

Corollary. The previous proposition holds with “left” replaced by “right.”

证明。转换角色fhgk在前面的命题中。

Proof. Switch the role of f with h and g with k in the previous proposition.

推论。如果f:XYg:YZ两者都有逆,那么(fg)1=g1f1

Corollary. If f:XY and g:YZ both have inverses, then (fg)1=g1f1.


15.2.单射、全射和双射函数

15.2. Injective, Surjective, and Bijective Functions

函数f:XY被称为单射,或单射,或一对一,如果给定任何x1x2A, 如果f(x1)=f(x2), 然后x1=x2。请注意,结论等同于它的逆否命题:如果x1x2, 然后f(x1)f(x2)。 所以f如果它映射不同的元素X到不同的元素Y

A function f:XY is said to be injective, or an injection, or one-one, if given any x1 and x2 in A, if f(x1)=f(x2), then x1=x2. Notice that the conclusion is equivalent to its contrapositive: if x1x2, then f(x1)f(x2). So f is injective if it maps distinct element of X to distinct elements of Y.

函数f:XY被称为是全射,或满射,或到上,如果对于每个元素yY,有一个xX使得f(x)=y。 换句话说,f如果余域中的每个元素都是f在域中的某个元素处。

A function f:XY is said to be surjective, or a surjection, or onto, if for every element y of Y, there is an x in X such that f(x)=y. In other words, f is surjective if every element in the codomain is the value of f at some element in the domain.

函数f:XY如果 既是单射又是单射,则称 为双射,或双,或一一对应。直观地讲,如果 之间存在双射XY, 然后XY大小相同,因为f使得每个元素X对应于Y反之亦然。例如,将“披头士乐队成员有四人”这一陈述解释为“集合{1,2,3,4}和集合{John, Paul, George, Ringo}。如果我们声称有五个披头士乐队成员,正如函数所证明的那样f它将 1 分配给约翰,2 分配给保罗,3 分配给乔治,4 分配给林戈,5 分配给约翰,你应该反对我们重复计算约翰——也就是说,f不是单射。如果我们声称只有三个披头士,正如函数f它将 1 分配给约翰,2 分配给保罗,3 分配给乔治,你应该反对我们漏掉了可怜的林戈——也就是说,f不是单射。

A function f:XY is said to be bijective, or a bijection, or a one-to-one correspondence, if it is both injective and surjective. Intuitively, if there is a bijection between X and Y, then X and Y have the same size, since f makes each element of X correspond to exactly one element of Y and vice-versa. For example, it makes sense to interpret the statement that there were four Beatles as the statement that there is a bijection between the set {1,2,3,4} and the set {John, Paul, George, Ringo}. If we claimed that there were five Beatles, as evidenced by the function f which assigns 1 to John, 2 to Paul, 3 to George, 4 to Ringo, and 5 to John, you should object that we double-counted John—that is, f is not injective. If we claimed there were only three Beatles, as evidenced by the function f which assigns 1 to John, 2 to Paul, and 3 to George, you should object that we left out poor Ringo—that is, f is not surjective.

接下来的两个命题表明,这些概念可以根据逆的存在来表述。

The next two propositions show that these notions can be cast in terms of the existence of inverses.


命题。f:XY

Proposition. Let f:XY.

  • 如果f有一个左逆,那么f是单射。

  • If f has a left inverse, then f is injective.

  • 如果f有一个右逆,那么f是全射。

  • If f has a right inverse, then f is surjective.

  • 如果f有一个逆,那么它就是f双射的。

  • If f has an inverse, then it is f bijective.

证明。对于第一个断言,假设f有一个左逆g,假设f(x1)=f(x2)。 然后g(f(x1))=g(f(x2)), 所以x1=x2

Proof. For the first claim, suppose f has a left inverse g, and suppose f(x1)=f(x2). Then g(f(x1))=g(f(x2)), and so x1=x2.

对于第二个主张,假设f有一个右逆h。 让y是任意元素Y,并让x=g(y)。 然后f(x)=f(g(y))=y

For the second claim, suppose f has a right inverse h. Let y be any element of Y, and let x=g(y). Then f(x)=f(g(y))=y.

第三个主张是根据前两个主张得出的。

The third claim follows from the first two.


以下命题更有趣,因为它要求我们定义新函数,给出以下假设f

The following proposition is more interesting, because it requires us to define new functions, given hypotheses on f.


命题。f:XY

Proposition. Let f:XY.

  • 如果X不为空,并且f是单射,那么f有一个左逆。

  • If X is nonempty and f is injective, then f has a left inverse.

  • 如果f是全射,那么f有一个右逆。

  • If f is surjective, then f has a right inverse.

  • 如果f如果是双射,那么它有一个逆。

  • If f if bijective, then it has an inverse.

证明。对于第一个断言,让x^是任意元素X,假设f是单射。定义g:YX通过设置g(y)等于任意x使得f(x)=y,如果有的话,x^否则。现在假设g(f(x))=x. 根据定义gx必须具有以下属性f(x)=f(x)。 自从f是单射,x=x, 所以g(f(x))=x

Proof. For the first claim, let x^ be any element of X, and suppose f is injective. Define g:YX by setting g(y) equal to any x such that f(x)=y, if there is one, and x^ otherwise. Now, suppose g(f(x))=x. By the definition of g, x has to have the property that f(x)=f(x). Since f is injective, x=x, so g(f(x))=x.

对于第二项主张,因为f是全射,我们知道对于每一个yY有任何x使得f(x)=y。 定义h:BA通过再次设置h(y)等于任何这样的x(与上一段相比,这里我们知道这样的x存在,但可能不是唯一的。)然后,根据定义h,我们有f(h(y))=y

For the second claim, because f is surjective, we know that for every y in Y there is any x such that f(x)=y. Define h:BA by again setting h(y) equal to any such x. (In contrast to the previous paragraph, here we know that such an x exists, but it might not be unique.) Then, by the definition of h, we have f(h(y))=y.


请注意g证明的第一部分要求函数“决定”是否存在xX使得f(x)=y。这个定义在数学上没有什么可疑之处,但在许多情况下,这无法通过算法来实现;换句话说,g可能无法从数据中计算出来。更有趣的是,h证明的第二部分要求函数“选择”一个合适的值x从众多候选者中挑选出一个。我们将在23.3 节中看到,这是选择公理的一个版本。在二十世纪初,选择公理在数学中的应用引起了激烈的争论,但今天它已经很普遍了。

Notice that the definition of g in the first part of the proof requires the function to “decide” whether there is an x in X such that f(x)=y. There is nothing mathematically dubious about this definition, but in many situations, this cannot be done algorithmically; in other words, g might not be computable from the data. More interestingly, the definition of h in the second part of the proof requires the function to “choose” a suitable value of x from among potentially many candidates. We will see in Section 23.3 that this is a version of the axiom of choice. In the early twentieth century, the use of the axiom of choice in mathematics was hotly debated, but today it is commonplace.

利用这些等价性和上一节的结果,我们可以证明以下内容:

Using these equivalences and the results in the previous section, we can prove the following:


命题。f:XBg:YZ

Proposition. Let f:XB and g:YZ.

  • 如果fg是单射,那么也是gf

  • If f and g are injective, then so is gf.

  • 如果fg是全射,那么gf

  • If f and g are surjective, then so is gf.

证明。如果fg是单射,那么它们有左逆hk分别在这种情况下hkgf. 第二个命题得到类似证明。

Proof. If f and g are injective, then they have left inverses h and k, respectively, in which case hk is a left inverse to gf. The second statement is proved similarly.


然而,我们可以证明这两个命题,而根本不需要提及逆命题。我们把这个留给你作为练习。

We can prove these two statements, however, without mentioning inverses at all. We leave that to you as an exercise.

请注意表达式f(n)=2n可以用来定义具有域的无穷多个函数N, 例如:

Notice that the expression f(n)=2n can be used to define infinitely many functions with domain N, such as:

  • 函数f:NN

  • a function f:NN

  • 函数f:NR

  • a function f:NR

  • 函数f:N{nn is even}

  • a function f:N{nn is even}

只有第三个函数是全射。因此,函数的余域和定义域的指定对于判断函数是否为全射至关重要。

Only the third one is surjective. Thus a specification of the function’s codomain as well as the domain is essential to making sense of whether a function is surjective.

15.3.域的函数和子集

15.3. Functions and Subsets of the Domain

认为f是一个函数XY。我们可能希望推理一下f在某个子集上AX。例如,我们可以说f 是可注入的 A如果对于每一个x1x2A, 如果f(x1)=f(x2), 然后x1=x2

Suppose f is a function from X to Y. We may wish to reason about the behavior of f on some subset A of X. For example, we can say that f is injective on A if for every x1 and x2 in A, if f(x1)=f(x2), then x1=x2.

如果f是一个函数XYAX,我们写f[A]表示图像 f A,定义为

If f is a function from X to Y and A is a subset of X, we write f[A] to denote the image of f on A, defined by

f[A]={yYy=f(x)for some x in A}.

换句话说,f[A]是元素的集合Y受到元素“打击”A在映射下f。请注意,这里有一个隐含的存在量词,因此关于图像的推理总是涉及相应的规则。

In words, f[A] is the set of elements of Y that are “hit” by elements of A under the mapping f. Notice that there is an implicit existential quantifier here, so that reasoning about images invariably involves the corresponding rules.


命题。假设f:XY, 和AX.那么对于任何xAf(x)位于f[A]

Proposition. Suppose f:XY, and A is a subset of X. Then for any x in A, f(x) is in f[A].

证明。根据定义,f(x)位于f[A]当且仅当存在一些xA使得f(x)=f(x).但这适用于x=x

Proof. By definition, f(x) is in f[A] if and only if there is some x in A such that f(x)=f(x). But that holds for x=x.

命题。假设f:XYg:YZ。 让A是……的子集X。 然后

Proposition. Suppose f:XY and g:YZ. Let A be a subset of X. Then

(gf)[A]=g[f[A]].

证明。假设z位于(gf)[A].然后对于一些xAz=(gf)(x)=g(f(x)). 根据前面的命题,f(x)位于f[A]. 再次根据先前的命题,g(f(x))位于g[f[A]]

Proof. Suppose z is in (gf)[A]. Then for some xA, z=(gf)(x)=g(f(x)). By the previous proposition, f(x) is in f[A]. Again by the previous proposition, g(f(x)) is in g[f[A]].

相反,假设z位于g[f[A]].然后有一个yf[A]使得f(y)=z,并且y位于f[D],有一个xA使得f(x)=y.但随后(gf)(x)=g(f(x))=g(y)=z, 所以z位于(gf)[A]

Conversely, suppose z is in g[f[A]]. Then there is a y in f[A] such that f(y)=z, and since y is in f[D], there is an x in A such that f(x)=y. But then (gf)(x)=g(f(x))=g(y)=z, so z is in (gf)[A].


请注意,如果f是一个函数XY, 然后f是全射当且仅当f[X]=Y。所以上述命题是对全射函数的复合是全射这一事实的推广。

Notice that if f is a function from X to Y, then f is surjective if and only if f[X]=Y. So the previous proposition is a generalization of the fact that the composition of surjective functions is surjective.

认为f是一个函数XY, 和AX.我们可以查看 f作为函数AY,只需忽略f在元素之外A。正确地说,这是另一个函数,表示为fA并称“限制fA换句话说,鉴于f:XYAXfA:AY是定义为的函数(fA)(x)=x对于每一个xA。请注意,现在“f是可注入的A” 意味着限制fA是单射。

Suppose f is a function from X to Y, and A is a subset of X. We can view f as a function from A to Y, by simply ignoring the behavior of f on elements outside of A. Properly speaking, this is another function, denoted fA and called “the restriction of f to A.” In other words, given f:XY and AX, fA:AY is the function defined by (fA)(x)=x for every x in A. Notice that now “f is injective on A” means simply that the restriction of f to A is injective.

函数还有另一个重要的运算,称为原像。如果f:XYBY那么 B 在下面 f,表示f1[B],定义为

There is another important operation on functions, known as the preimage. If f:XY and BY, then the preimage of B under f, denoted f1[B], is defined by

f1[B]={xXf(x)B},

即元素集X被映射到B。请注意,即使f没有逆;对于给定的yB,可能没有x拥有该属性f(x)B,或者可能有很多。如果f有一个逆,f1,那么对于每一个yB只有一个xX与财产f(x)B,在这种情况下,f1[B]不管你把它理解为B在下面f1或原像B在下面f

that is, the set of elements of X that get mapped into B. Notice that this makes sense even if f does not have an inverse; for a given y in B, there may be no x’s with the property f(x)B, or there may be many. If f has an inverse, f1, then for every y in B there is exactly one xX with the property f(x)B, in which case, f1[B] means the same thing whether you interpret it as the image of B under f1 or the preimage of B under f.


命题。假设f:XYg:YZ。 让C是……的子集Z。 然后

Proposition. Suppose f:XY and g:YZ. Let C be a subset of Z. Then

(gf)1[C]=f1[g1[C]].

证明。对于任何yCy位于(gf)1[C]当且仅当g(f(y))位于C. 反过来,这种情况发生当且仅当f(y)位于g1[C],这反过来又发生在当且仅当y位于f1[g1[C]]

Proof. For any y in C, y is in (gf)1[C] if and only if g(f(y)) is in C. This, in turn, happens if and only if f(y) is in g1[C], which in turn happens if and only if y is in f1[g1[C]].


这里我们给出了图像和原像的一系列事实属性。这里,f表示来自的任意函数XYA,A1,A2,表示任意子集X, 和B,B1,B2,表示任意子集Y

Here we give a long list of facts properties of images and preimages. Here, f denotes an arbitrary function from X to Y, A,A1,A2, denote arbitrary subsets of X, and B,B1,B2, denote arbitrary subsets of Y.

  • Af1[f[A]],如果f是单射,A=f1[f[A]]

  • Af1[f[A]], and if f is injective, A=f1[f[A]].

  • f[f1[B]]B,如果f是全射,B=f[f1[B]]

  • f[f1[B]]B, and if f is surjective, B=f[f1[B]].

  • 如果A1A2, 然后f[A1]f[A2]

  • If A1A2, then f[A1]f[A2].

  • 如果B1B2, 然后f1[B1]f1[B2]

  • If B1B2, then f1[B1]f1[B2].

  • f[A1A2]=f[A1]f[A2]

  • f[A1A2]=f[A1]f[A2].

  • f1[B1B2]=f1[B1]f1[B2]

  • f1[B1B2]=f1[B1]f1[B2].

  • f[A1A2]f[A1]f[A2],如果f是单射,f[A1A2]=f[A1]f[A2]

  • f[A1A2]f[A1]f[A2], and if f is injective, f[A1A2]=f[A1]f[A2].

  • f1[B1B2]=f1[B1]f1[B2]

  • f1[B1B2]=f1[B1]f1[B2].

  • f[A1]f[A2]f[A1A2]

  • f[A1]f[A2]f[A1A2].

  • f1[B1]f1[B2]f1[B1B2]

  • f1[B1]f1[B2]f1[B1B2].

  • f[A]B=f[Af1[B]]

  • f[A]B=f[Af1[B]].

  • f[A]Bf[Af1[B]]

  • f[A]Bf[Af1[B]].

  • Af1[B]f1[f[A]B]

  • Af1[B]f1[f[A]B].

  • Af1[B]f1[f[A]B]

  • Af1[B]f1[f[A]B].

证明这样的恒等式通常需要展开定义并使用基本的逻辑推理。以下是一个例子。

Proving identities like this is typically a matter of unfolding definitions and using basic logical inferences. Here is an example.


命题。XY是集合,f:XYAX, 和BY。 然后f[A]B=f[Af1[B]]

Proposition. Let X and Y be sets, f:XY, AX, and BY. Then f[A]B=f[Af1[B]].

证明。假设yf[A]B。 然后yB,对于一些xAf(x)=y.但这意味着x位于f1[B], 所以xAf1[B]。 自从f(x)=y,我们有yf[Af1[B]],根据需要。

Proof. Suppose yf[A]B. Then yB, and for some xA, f(x)=y. But this means that x is in f1[B], and so xAf1[B]. Since f(x)=y, we have yf[Af1[B]], as needed.

相反,假设yf[Af1[B]].然后对于一些xAf1[B],我们有f(x)=y。 为了这x, 有xAf(x)B。 自从f(x)=y,我们有yB,并且xA,我们还有yf[A],按要求。

Conversely, suppose yf[Af1[B]]. Then for some xAf1[B], we have f(x)=y. For this x, have xA and f(x)B. Since f(x)=y, we have yB, and since xA, we also have yf[A], as required.


15.4.函数和关系

15.4. Functions and Relations

二元关系R(x,y)AB函数如果对于每一个xA存在一个独特的yB使得R(x,y)。 如果R是函数关系,我们可以定义一个函数fR:XB通过设置fR(x)等于独一无二的yB使得R(x,y)。相反,不难看出,如果f:XB是任意函数,关系Rf(x,y)定义为f(x)=y是函数关系。关系Rf(x,y)称为​f

A binary relation R(x,y) on A and B is functional if for every x in A there exists a unique y in B such that R(x,y). If R is a functional relation, we can define a function fR:XB by setting fR(x) to be equal to the unique y in B such that R(x,y). Conversely, it is not hard to see that if f:XB is any function, the relation Rf(x,y) defined by f(x)=y is a functional relation. The relation Rf(x,y) is known as the graph of f.

不难检验函数和关系是成对出现的:如果f是与函数关系相关的函数R, 然后R是与函数相关的函数关系f,反之亦然。在集合论基础中,函数通常被定义为函数关系。相反,我们已经看到,在像 Lean 采用的类型论基础中,关系通常被定义为某些类型的函数。我们稍后会讨论这些问题,同时只需指出,在日常数学实践中,基础细节并不那么重要;重要的是每个函数都有一个图,并且任何函数关系都可用于定义相应的函数。

It is not hard to check that functions and relations travel in pairs: if f is the function associated with a functional relation R, then R is the functional relation associated the function f, and vice-versa. In set-theoretic foundations, a function is often defined to be a functional relation. Conversely, we have seen that in type-theoretic foundations like the one adopted by Lean, relations are often defined to be certain types of functions. We will discuss these matters later on, and in the meanwhile only remark that in everyday mathematical practice, the foundational details are not so important; what is important is simply that every function has a graph, and that any functional relation can be used to define a corresponding function.

到目前为止,我们一直关注于接受单个参数的函数。我们也可以考虑函数f(x,y)或者g(x,y,z)接受多个参数。例如,加法函数f(x,y)=x+y整数上的函数接受两个整数并返回一个整数。记住,我们可以考虑二元函数、三元函数等等,函数的参数数量称为其“元数”。理解具有多个参数的函数的一种简单方法是将它们视为笛卡尔积的一元函数。我们可以考虑一个函数f它接受两个参数,一个在A一个B并返回一个参数C作为一元函数A×BC,由此f(a,b)缩写f((a,b))我们已经看到,在依赖类型理论(以及精益理论)中,考虑这样的函数更为方便f作为接受元素的函数A并返回一个函数BC, 以便f(a,b)缩写(f(a))(b).这样的功能f地图ABC, 在哪里BC是来自的函数集BC

So far, we have been focusing on functions that take a single argument. We can also consider functions f(x,y) or g(x,y,z) that take multiple arguments. For example, the addition function f(x,y)=x+y on the integers takes two integers and returns an integer. Remember, we can consider binary functions, ternary functions, and so on, and the number of arguments to a function is called its “arity.” One easy way to make sense of functions with multiple arguments is to think of them as unary functions from a cartesian product. We can think of a function f which takes two arguments, one in A and one in B, and returns an argument in C as a unary function from A×B to C, whereby f(a,b) abbreviates f((a,b)). We have seen that in dependent type theory (and in Lean) it is more convenient to think of such a function f as a function which takes an element of A and returns a function from BC, so that f(a,b) abbreviates (f(a))(b). Such a function f maps A to BC, where BC is the set of functions from B to C.

稍后,当我们考虑集合论和类型论基础时,我们将回到这些对更高元数函数进行建模的不同方法。我们再次指出,在普通数学中,基础细节并不重要。上述两种选择是可相互翻译的,并且认可对函数进行非形式化推理的相同原则。

We will return to these different ways of modeling functions of higher arity later on, when we consider set-theoretic and type-theoretic foundations. One again, we remark that in ordinary mathematics, the foundational details do not matter much. The two choices above are inter-translatable, and sanction the same principles for reasoning about functions informally.

在数学中,我们经常考虑偏函数的概念XY,它实际上是来自某个子集的函数XY. 事实是f是来自的偏函数XY有时写成f:XY,这应该被解释为f:AY对于某个子集AY直观地,我们认为f作为函数XY它的某些输入只是“未定义”;例如,我们可以想到f:RR定义为f(x)=1/x,未定义于x=0,因此实际上f:R{0}R. 该套装A有时被称为 f,在这种情况下,没有好名字X;其他人继续呼叫X域,并参考A作为定义的域。为了表明函数f定义在x,也就是说x在定义域内f,我们有时会写f(x)。 如果fg两个部分函数来自XY,我们写f(x)g(x)意味着fg都定义在x并具有相同的值,或均未定义x. 单射性、全射性和组合性的概念被扩展到部分函数,​​一般来说就像你所期望的那样。

In mathematics, we often also consider the notion of a partial function from X to Y, which is really a function from some subset of X to Y. The fact that f is a partial function from X to Y is sometimes written f:XY, which should be interpreted as saying that f:AY for some subset A of Y. Intuitively, we think of f as a function from XY which is simply “undefined” at some of its inputs; for example, we can think of f:RR defined by f(x)=1/x, which is undefined at x=0, so that in reality f:R{0}R. The set A is sometimes called the domain of f, in which case, there is no good name for X; others continue to call X the domain, and refer to A as the domain of definition. To indicate that a function f is defined at x, that is, that x is in the domain of definition of f, we sometimes write f(x). If f and g are two partial functions from X to Y, we write f(x)g(x) to mean that either f and g are both defined at x and have the same value, or are both undefined at x. Notions of injectivity, surjectivity, and composition are extended to partial functions, generally as you would expect them to be.

就关系而言,偏函数f对应于关系Rf(x,y)对于每一个x最多有一个y使得Rf(x,y)成立。数学家有时也会考虑来自XY,对应于关系Rf(x,y)对于每一个xX,至少有一个y使得Rf(x,y)成立。可能有很多这样的y;你可以把它们看作具有多个输出值的函数。如果你仔细思考一下,就会发现部分多重函数本质上只不过是一种任意关系。

In terms of relations, a partial function f corresponds to a relation Rf(x,y) such that for every x there is at most one y such that Rf(x,y) holds. Mathematicians also sometimes consider multifunctions from X to Y, which correspond to relations Rf(x,y) such that for every x in X, there is at least one y such that Rf(x,y) holds. There may be many such y; you can think of these as functions which have more than one output value. If you think about it for a moment, you will see that a partial multifunction is essentially nothing more than an arbitrary relation.

15.5.练习

15.5. Exercises

  1. f是来自的任何函数XY,并让g是来自的任何函数YZ

    • 表明如果gf是单射,那么f是单射。

    • 举一个函数的例子fg如上所述,这样gf是单射,但是g不是单射。

    • 表明如果gf是单射,并且f是全射,那么g是单射。

  2. Let f be any function from X to Y, and let g be any function from Y to Z.

    • Show that if gf is injective, then f is injective.

    • Give an example of functions f and g as above, such that that gf is injective, but g is not injective.

    • Show that if gf is injective and f is surjective, then g is injective.

  3. fg和上一个问题一样。假设gf是全射。

    • f一定是全射吗?要么证明它是全射,要么给出反例。

    • g一定是全射吗?要么证明它是全射,要么给出反例。

  4. Let f and g be as in the last problem. Suppose gf is surjective.

    • Is f necessarily surjective? Either prove that it is, or give a counterexample.

    • Is g necessarily surjective? Either prove that it is, or give a counterexample.

  5. 函数fRR被称为 严格增加,如果x1<x2f(x1)<f(x2)

    • 表明如果f:RR是严格递增的,那么它是单射(因此它有一个左逆)。

    • 表明如果f:RR严格增加,并且gf, 然后g是严格递增的。

  6. A function f from R to R is said to be strictly increasing if whenever x1<x2, f(x1)<f(x2).

    • Show that if f:RR is strictly increasing, then it is injective (and hence it has a left inverse).

    • Show that if f:RR is strictly increasing, and g is a right inverse to f, then g is strictly increasing.

  7. f:XY是任意函数,设AB是子集X. 表明f[AB]=f[A]f[B]

  8. Let f:XY be any function, and let A and B be subsets of X. Show that f[AB]=f[A]f[B].

  9. f:XY是任意函数,设AB是任意子集X。 展示f[A]f[B]f[AB]

  10. Let f:XY be any function, and let A and B be any subsets of X. Show f[A]f[B]f[AB].

  11. 定义二元关系的组合和逆的概念,以概括函数的概念。

  12. Define notions of composition and inverse for binary relations that generalize the notions for functions.

16. Lean 中的函数

16. Functions in Lean

16.1.函数和符号逻辑

16.1. Functions and Symbolic Logic

现在让我们从形式上来考虑函数。尽管我们在上一章的定义中避免使用量词和逻辑符号,但现在你应该已经看到它们潜伏在表面之下。事实上,两个函数f,g:XY当且仅当它们在每个输入上采用相同的值时才相等,可以表示如下:

Let us now consider functions in formal terms. Even though we have avoided the use of quantifiers and logical symbols in the definitions in the last chapter, by now you should be seeing them lurking beneath the surface. That fact that two functions f,g:XY are equal if and only if they take the same values at every input can be expressed as follows:

xX(f(x)=g(x))f=g.

这一原理被称为函数外延性,类似于第 12.1 节讨论的集合外延性原理。回想一下符号xXP(x)缩写x(xXP(x)), 和xXP(x)缩写x(xXP(x)),从而将量词相对化为X

This principle is a known as function extensionality, analogous to the principle of extensionality for sets, discussed in Section 12.1. Recall that the notation xXP(x) abbreviates x(xXP(x)), and xXP(x) abbreviates x(xXP(x)), thereby relativizing the quantifiers to X.

如果我们假设我们正在以逻辑形式主义的方式工作,并且基本类型为XY,这样我们就可以指定x范围超过X。在这种情况下,我们将改为写

We can avoid set-theoretic notation if we assume we are working in a logical formalism with basic types for X and Y, so that we can specify that x ranges over X. In that case, we will write instead

x:X(f(x)=g(x)f=g)

表示量化结束X。从今以后,我们将假设所有变量都涵盖某种类型,尽管有时当可以从上下文推断出类型时,我们会省略量词中的类型。

to indicate that the quantification is over X. Henceforth, we will assume that all our variables range over some type, though we will sometimes omit the types in the quantifiers when they can be inferred from context.

函数f如果满足

The function f is injective if it satisfies

x1,x2:X(f(x1)=f(x2)x1=x2),

f是全射,如果

and f is surjective if

y:Yx:Xf(x)=y.

如果f:XYg:YXgf如果

If f:XY and g:YX, g is a left inverse to f if

x:Xg(f(x))=x.

请注意,这是一个通用语句,它相当于fg

Notice that this is a universal statement, and it is equivalent to the statement that f is a right inverse to g.

请记住,在逻辑中,通常使用 lambda 符号来定义函数。我们可以将恒等函数表示为λxx或许λx:Xx强调函数的定义域是X。 如果f:XYg:YZ,我们可以定义合成gf经过gf=λx:Xg(f(x))

Remember that in logic it is common to use lambda notation to define functions. We can denote the identity function by λxx, or perhaps λx:Xx to emphasize that the domain of the function is X. If f:XY and g:YZ, we can define the composition gf by gf=λx:Xg(f(x)).

还要记住,如果P(x)是任何谓词,那么在一阶逻辑中,我们可以断言存在一个唯一的x令人满意P(x),书面!xP(x),与以下两个语句结合:

Also remember that if P(x) is any predicate, then in first-order logic we can assert that there exists a unique x satisfying P(x), written !xP(x), with the conjunction of the following two statements:

  • xP(x)

  • xP(x)

  • x1,x2(P(x1)P(x2)x1=x2)

  • x1,x2(P(x1)P(x2)x1=x2)

等价地,我们可以写

Equivalently, we can write

(P(x)x(P(x)x=x)).

假设!xP(x),以下两个语句是等价的:

Assuming !xP(x), the following two statements are equivalent:

  • x(P(x)Q(x))

  • x(P(x)Q(x))

  • x(P(x)Q(x))

  • x(P(x)Q(x))

两者都可以被认为断言“x令人满意P也满足Q

and both can be taken to assert that “the x satisfying P also satisfies Q.”

二元关系RXY满足以下条件则为函数式

A binary relation R on X and Y is functional if it satisfies

x!yR(x,y).

在这种情况下,逻辑学家可能会使用iota 符号

In that case, a logician might use iota notation,

f(x)=ιyR(x,y)

定义f(x)等于独一无二的y令人满意R(x,y)。 如果R满足较弱的属性

to define f(x) to be equal to the unique y satisfying R(x,y). If R satisfies the weaker property

xyR(x,y),

逻辑学家可能会使用希尔伯特 epsilon来定义函数

a logician might use the Hilbert epsilon to define a function

f(x)=εyR(x,y)

“选择”一个值y令人满意R(x,y). 正如我们上面所指出的,这是选择公理的隐含用法。

to “choose” a value of y satisfying R(x,y). As we have noted above, this is an implicit use of the axiom of choice.

16.2.二阶及高阶逻辑

16.2. Second- and Higher-Order Logic

与一阶逻辑相比,我们从固定的函数和关系符号开始,而前几章中讨论的主题鼓励我们考虑一种更具表达力的语言,其变量也涵盖函数和关系。例如,假设一个函数f:XY有一个左逆隐式地涉及函数的量化,

In contrast to first-order logic, where we start with a fixed stock of function and relation symbols, the topics we have been considering in the last few chapters encourage us to consider a more expressive language with variables ranging over functions and relations as well. For example, saying that a function f:XY has a left-inverse implicitly involves a quantifying over functions,

gxg(f(x))=x.

该定理断言,如果任何函数fXY是单射,那么它有一个左逆,可以表示如下:

The theorem that asserts that if any function f from X to Y is injective then it has a left-inverse can be expressed as follows:

x1,x2(f(x1)=f(x2)x1=x2)gxg(f(x))=x.

类似地,说两个集合XY具有一一对应关系断言函数的存在f:XY以及逆f再如,在第15.4节中我们断言每一个函数关系都会产生一个对应的函数,反之亦然。

Similarly, saying that two sets X and Y have a one-to-one correspondence asserts the existence of a function f:XY as well as an inverse to f. For another example, in Section 15.4 we asserted that every functional relation gives rise to a corresponding function, and vice-versa.

这些陈述之所以有趣,是因为它们涉及函数和关系上的存在量化和通用量化。这超出了第一阶逻辑的范围。一种选择是用第一阶逻辑的语言开发一种理论,其中宇宙包含函数和关系作为对象;我们稍后会看到这就是公理集合论所做的。另一种方法是扩展第一阶逻辑以涉及新类型的量词和变量,以涵盖函数和关系。这就是高阶逻辑所做的。

What makes these statements interesting is that they involve quantification, both existential and universal, over functions and relations. This takes us outside the realm of first-order logic. One option is to develop a theory in the language of first-order logic in which the universe contains functions and relations as objects; we will see later that this is what axiomatic set theory does. An alternative is to extend first-order logic to involve new kinds of quantifiers and variables, to range over functions and relations. This is what higher-order logic does.

有多种方法可以实现这一点。鉴于前面描述的函数和关系之间的关系,人们可以将关系作为基础,并根据关系定义函数,反之亦然。以下高阶逻辑的表述,由逻辑学家阿隆佐·丘奇提出,遵循后一种方法。它有时被称为简单类型理论

There are various ways to go about this. In view of the relationship between functions and relations described earlier, one can take relations as basic, and define functions in terms of them, or vice-versa. The following formulation of higher-order logic, due to the logician Alonzo Church, follows the latter approach. It is sometimes known as simple type theory.

从一些基本类型开始,X,Y,Z,还有一种特殊类型,Prop,命题。添加以下两个规则来构建新类型:

Start with some basic types, X,Y,Z, and a special type, Prop, of propositions. Add the following two rules to build new types:

  • 如果UV是类型,所以是U×V

  • If U and V are types, so is U×V.

  • 如果UV是类型,所以是UV

  • If U and V are types, so is UV.

第一个旨在表示有序对的类型(u,v), 在哪里u位于Uv位于V. 第二个旨在表示函数的类型UV简单类型理论现在添加了以下形成表达式的方法:

The first intended to denote the type of ordered pairs (u,v), where u is in U and v is in V. The second is intended to denote the type of functions from U to V. Simple type theory now adds the following means of forming expressions:

  • 如果u属于类型Uv属于类型V(u,v)属于类型U×V

  • If u is of type U and v is of type V, (u,v) is of type U×V.

  • 如果p属于类型U×V, 然后(p)1属于类型U(p)2如果类型V. (这些旨在表示对中的第一个和第二个元素p

  • If p is of type U×V, then (p)1 is of type U and (p)2 if of type V. (These are intended to denote the first and second element of the pair p.)

  • 如果x是类型的变量U, 和v是任何类型的表达式V, 然后λxv属于类型UV

  • If x is a variable of type U, and v is any expression of type V, then λxv is of type UV.

  • 如果f属于类型UVu属于类型Uf(u)属于类型V

  • If f is of type UV and u is of type U, f(u) is of type V.

此外,简单类型理论提供了我们在一阶逻辑中拥有的所有方法来构建命题——布尔连接词、量词和相等性。

In addition, simple type theory provides all the means we have in first-order logic—boolean connectives, quantifiers, and equality—to build propositions.

函数f(x,y)其采用XY到一种类型Z被视为类型对象X×YZ. 类似地,二元关系R(x,y)XY被视为类型对象X×YProp。高阶逻辑之所以“高阶”,是因为我们可以无限迭代函数类型操作。例如,如果N是自然数的类型,NN表示从自然数到自然数的函数类型,并且(NN)N表示函数类型F(f)它以函数作为参数,并返回一个自然数。

A function f(x,y) which takes elements of X and Y to a type Z is viewed as an object of type X×YZ. Similarly, a binary relation R(x,y) on X and Y is viewed as an object of type X×YProp. What makes higher-order logic “higher order” is that we can iterate the function type operation indefinitely. For example, if N is the type of natural numbers, NN denotes the type of functions from the natural numbers to the natural numbers, and (NN)N denotes the type of functions F(f) which take a function as argument, and return a natural number.

我们没有非常仔细地指定高阶逻辑的语法和规则。这在许多更高级的逻辑教科书中都是这样做的。高阶逻辑的一部分只允许基本类型的函数和关系(不迭代这些构造),称为二阶逻辑。

We have not specified the syntax and rules of higher-order logic very carefully. This is done in a number of more advanced logic textbooks. The fragment of higher-order logic which allows only functions and relations on the basic types (without iterating these constructions) is known as second-order logic.

这些概念应该看起来很熟悉;我们在精益中一直使用这些结构,并带有类似的符号。事实上,精益的逻辑是一种更加复杂和富有表现力的逻辑系统,它完全涵盖了我们在此讨论的所有高阶逻辑概念。

These notions should seem familiar; we have been using these constructions, with similar notation, in Lean. Indeed, Lean’s logic is an even more elaborate and expressive system of logic, which fully subsumes all the notions of higher-order logic we have discussed here.

16.3. Lean 中的函数

16.3. Functions in Lean

我们讨论的概念具有如此简单的逻辑形式,这意味着在精益中定义它们很容易。精益中的正式表示和上述非正式表示之间的主要区别在于,在精益中,我们区分了类型X和该类型的子集。A : set X

The fact that the notions we have been discussing have such a straightforward logical form means that it is easy to define them in Lean. The main difference between the formal representation in Lean and the informal representation above is that, in Lean, we distinguish between a type X and a subset A : set X of that type.

在Lean的库中,组合和身份定义如下:

In Lean’s library, composition and identity are defined as follows:

variables {X Y Z : Type}

def comp (f : Y  Z) (g : X  Y) : X  Z :=
λx, f (g x)

infixr  `  ` := comp

def id (x : X) : X :=
x
variables {X Y Z : Type}

def comp (f : Y  Z) (g : X  Y) : X  Z :=
λx, f (g x)

infixr  `  ` := comp

def id (x : X) : X :=
x

通常我们用funext(表示“函数外延性”)来证明两个函数相等。

Ordinarily, we use funext (for “function extensionality”) to prove that two functions are equal.

example (f g : X  Y) (h :  x, f x = g x) : f = g :=
funext h
example (f g : X  Y) (h :  x, f x = g x) : f = g :=
funext h

但是,精益可以通过简单地展开定义和简化表达式,利用反身性来证明一些基本恒等式。

But Lean can prove some basic identities by simply unfolding definitions and simplifying expressions, using reflexivity.

lemma left_id (f : X  Y) : id  f = f := rfl

lemma right_id (f : X  Y) : f  id = f := rfl

theorem comp.assoc (f : Z  W) (g : Y  Z) (h : X  Y) :
  (f  g)  h = f  (g  h) := rfl

theorem comp.left_id (f : X  Y) : id  f = f := rfl

theorem comp.right_id (f : X  Y) : f  id = f := rfl
lemma left_id (f : X  Y) : id  f = f := rfl

lemma right_id (f : X  Y) : f  id = f := rfl

theorem comp.assoc (f : Z  W) (g : Y  Z) (h : X  Y) :
  (f  g)  h = f  (g  h) := rfl

theorem comp.left_id (f : X  Y) : id  f = f := rfl

theorem comp.right_id (f : X  Y) : f  id = f := rfl

我们可以定义它的含义f为单射、全射或双射:

We can define what it means for f to be injective, surjective, or bijective:

def injective (f : X  Y) : Prop :=
 x₁ x₂⦄, f x₁ = f x₂  x₁ = x₂

def surjective (f : X  Y) : Prop :=
 y,  x, f x = y

def bijective (f : X  Y) := injective f  surjective f
def injective (f : X  Y) : Prop :=
 x₁ x₂⦄, f x₁ = f x₂  x₁ = x₂

def surjective (f : X  Y) : Prop :=
 y,  x, f x = y

def bijective (f : X  Y) := injective f  surjective f

在 的定义中隐含地标记变量x₁和意味着我们不必经常写它们。具体来说,给定,和,我们写而不是来显示。x₂injectiveh : injective fh₁ : f x₁ = f x₂h h₁h x₁ x₂ h₁x₁ = x₂

Marking the variables x₁ and x₂ implicit in the definition of injective means that we do not have to write them as often. Specifically, given h : injective f, and h₁ : f x₁ = f x₂, we write h h₁ rather than h x₁ x₂ h₁ to show x₁ = x₂.

然后我们可以证明恒等函数是双射的:

We can then prove that the identity function is bijective:

theorem injective_id : injective (@id X) :=
assume x₁ x₂,
assume H : id x₁ = id x₂,
show x₁ = x₂, from H

theorem surjective_id : surjective (@id X) :=
assume y,
show  x, id x = y, from exists.intro y rfl

theorem bijective_id : bijective (@id X) :=
and.intro injective_id surjective_id
theorem injective_id : injective (@id X) :=
assume x₁ x₂,
assume H : id x₁ = id x₂,
show x₁ = x₂, from H

theorem surjective_id : surjective (@id X) :=
assume y,
show  x, id x = y, from exists.intro y rfl

theorem bijective_id : bijective (@id X) :=
and.intro injective_id surjective_id

更有趣的是,我们可以证明单射函数的复合是单射,等等。

More interestingly, we can prove that the composition of injective functions is injective, and so on.

theorem injective_comp {g : Y  Z} {f : X  Y}
    (Hg : injective g) (Hf : injective f) :
  injective (g  f) :=
assume x₁ x₂,
assume : (g  f) x₁ = (g  f) x₂,
have f x₁ = f x₂, from Hg this,
show x₁ = x₂, from Hf this

theorem surjective_comp {g : Y  Z} {f : X  Y}
    (hg : surjective g) (hf : surjective f) :
  surjective (g  f) :=
assume z,
exists.elim (hg z) $
assume y (hy : g y = z),
exists.elim (hf y) $
assume x (hx : f x = y),
have g (f x) = z, from eq.subst (eq.symm hx) hy,
show  x, g (f x) = z, from exists.intro x this

theorem bijective_comp {g : Y  Z} {f : X  Y}
    (hg : bijective g) (hf : bijective f) :
  bijective (g  f) :=
have ginj : injective g, from hg.left,
have gsurj : surjective g, from hg.right,
have finj : injective f, from hf.left,
have fsurj : surjective f, from hf.right,
and.intro (injective_comp ginj finj)
  (surjective_comp gsurj fsurj)
theorem injective_comp {g : Y  Z} {f : X  Y}
    (Hg : injective g) (Hf : injective f) :
  injective (g  f) :=
assume x₁ x₂,
assume : (g  f) x₁ = (g  f) x₂,
have f x₁ = f x₂, from Hg this,
show x₁ = x₂, from Hf this

theorem surjective_comp {g : Y  Z} {f : X  Y}
    (hg : surjective g) (hf : surjective f) :
  surjective (g  f) :=
assume z,
exists.elim (hg z) $
assume y (hy : g y = z),
exists.elim (hf y) $
assume x (hx : f x = y),
have g (f x) = z, from eq.subst (eq.symm hx) hy,
show  x, g (f x) = z, from exists.intro x this

theorem bijective_comp {g : Y  Z} {f : X  Y}
    (hg : bijective g) (hf : bijective f) :
  bijective (g  f) :=
have ginj : injective g, from hg.left,
have gsurj : surjective g, from hg.right,
have finj : injective f, from hf.left,
have fsurj : surjective f, from hf.right,
and.intro (injective_comp ginj finj)
  (surjective_comp gsurj fsurj)

左逆和右逆的概念按照预期的方式定义。

The notions of left and right inverse are defined in the expected way.

-- g is a left inverse to f
def left_inverse (g : Y  X) (f : X  Y) : Prop :=
 x, g (f x) = x

-- g is a right inverse to f
def right_inverse (g : Y  X) (f : X  Y) : Prop :=
left_inverse f g
-- g is a left inverse to f
def left_inverse (g : Y  X) (f : X  Y) : Prop :=
 x, g (f x) = x

-- g is a right inverse to f
def right_inverse (g : Y  X) (f : X  Y) : Prop :=
left_inverse f g

具体来说,与左逆或右逆组合可得到恒等式。

In particular, composing with a left or right inverse yields the identity.

def id_of_left_inverse {g : Y  X} {f : X  Y} :
  left_inverse g f  g  f = id :=
assume H, funext H

def id_of_right_inverse {g : Y  X} {f : X  Y} :
  right_inverse g f  f  g = id :=
assume H, funext H
def id_of_left_inverse {g : Y  X} {f : X  Y} :
  left_inverse g f  g  f = id :=
assume H, funext H

def id_of_right_inverse {g : Y  X} {f : X  Y} :
  right_inverse g f  f  g = id :=
assume H, funext H

注意,我们需要用funext来证明函数的相等性。

Notice that we need to use funext to show the equality of functions.

下面证明,如果一个函数有左逆,那么它就是单射,如果它有右逆,那么它就是全射。

The following shows that if a function has a left inverse, then it is injective, and if it has a right inverse, then it is surjective.

theorem injective_of_left_inverse {g : Y  X} {f : X  Y} :
  left_inverse g f  injective f :=
assume h, assume x₁ x₂, assume feq,
calc x₁ = g (f x₁) : by rw h
    ... = g (f x₂) : by rw feq
    ... = x₂       : by rw h

theorem surjective_of_right_inverse {g : Y   X} {f : X  Y} :
  right_inverse g f  surjective f :=
assume h, assume y,
let  x : X := g y in
have f x = y, from calc
  f x  = (f (g y))    : rfl
   ... = y            : by rw [h y],
show  x, f x = y, from exists.intro x this
theorem injective_of_left_inverse {g : Y  X} {f : X  Y} :
  left_inverse g f  injective f :=
assume h, assume x₁ x₂, assume feq,
calc x₁ = g (f x₁) : by rw h
    ... = g (f x₂) : by rw feq
    ... = x₂       : by rw h

theorem surjective_of_right_inverse {g : Y   X} {f : X  Y} :
  right_inverse g f  surjective f :=
assume h, assume y,
let  x : X := g y in
have f x = y, from calc
  f x  = (f (g y))    : rfl
   ... = y            : by rw [h y],
show  x, f x = y, from exists.intro x this

16.4.经典地定义逆

16.4. Defining the Inverse Classically

上一节列出的所有定理都可以在 Lean 库中找到,当您使用以下命令打开函数命名空间时,它们都可供您使用:open function

All the theorems listed in the previous section are found in the Lean library, and are available to you when you open the function namespace with open function:

open function

#check comp
#check left_inverse
#check has_right_inverse
open function

#check comp
#check left_inverse
#check has_right_inverse

然而,定义反函数需要经典推理,我们可以通过打开经典命名空间来获得:

Defining inverse functions, however, requires classical reasoning, which we get by opening the classical namespace:

open classical

section
  variables A B : Type
  variable P : A  Prop
  variable R : A  B  Prop

  example : ( x,  y, R x y)   f : A  B,  x, R x (f x) :=
  axiom_of_choice

  example (h :  x, P x) : P (some h) :=
  some_spec h
end
open classical

section
  variables A B : Type
  variable P : A  Prop
  variable R : A  B  Prop

  example : ( x,  y, R x y)   f : A  B,  x, R x (f x) :=
  axiom_of_choice

  example (h :  x, P x) : P (some h) :=
  some_spec h
end

选择公理告诉我们,如果对于每个,都有满足 的,那么就存在一个函数,它对于每个 都选择这样的。在精益中,这个“公理”是使用经典构造来证明的,即函数(有时称为“不定描述运算符”),给定某个满足 的,返回这样的。有了这些构造,反函数定义如下:x : Xy : YR x yf : X YxysomexP xx

The axiom of choice tells us that if, for every x : X, there is a y : Y satisfying R x y, then there is a function f : X Y which, for every x chooses such a y. In Lean, this “axiom” is proved using a classical construction, the some function (sometimes called “the indefinite description operator”) which, given that there is some x satisfying P x, returns such an x. With these constructions, the inverse function is defined as follows:

open classical function
local attribute [instance] prop_decidable

variables {X Y : Type}

noncomputable def inverse (f : X  Y) (default : X) : Y  X :=
λ y, if h :  x, f x = y then some h else default
open classical function
local attribute [instance] prop_decidable

variables {X Y : Type}

noncomputable def inverse (f : X  Y) (default : X) : Y  X :=
λ y, if h :  x, f x = y then some h else default

精益要求我们承认该定义不是计算性的,因为首先,从算法上来说可能无法决定条件是否h成立,即使成立,从算法上来说也可能无法找到合适的值x

Lean requires us to acknowledge that the definition is not computational, since, first, it may not be algorithmically possible to decide whether or not condition h holds, and even if it does, it may not be algorithmically possible to find a suitable value of x.

下面,命题inverse_of_exists断言inverse满足其规范,后续定理表明,如果f是单射,则inverse函数确实是左逆。

Below, the proposition inverse_of_exists asserts that inverse meets its specification, and the subsequent theorem shows that if f is injective, then the inverse function really is a left inverse.

theorem inverse_of_exists (f : X  Y) (default : X) (y : Y)
  (h :  x, f x = y) :
f (inverse f default y) = y :=
have h1 : inverse f default y = some h, from dif_pos h,
have h2 : f (some h) = y, from some_spec h,
eq.subst (eq.symm h1) h2

theorem is_left_inverse_of_injective (f : X  Y) (default : X)
  (injf : injective f) :
left_inverse (inverse f default) f :=
let finv := (inverse f default) in
assume x,
have h1 :  x', f x' = f x, from exists.intro x rfl,
have h2 : f (finv (f x)) = f x, from inverse_of_exists f default (f x) h1,
show finv (f x) = x, from injf h2
theorem inverse_of_exists (f : X  Y) (default : X) (y : Y)
  (h :  x, f x = y) :
f (inverse f default y) = y :=
have h1 : inverse f default y = some h, from dif_pos h,
have h2 : f (some h) = y, from some_spec h,
eq.subst (eq.symm h1) h2

theorem is_left_inverse_of_injective (f : X  Y) (default : X)
  (injf : injective f) :
left_inverse (inverse f default) f :=
let finv := (inverse f default) in
assume x,
have h1 :  x', f x' = f x, from exists.intro x rfl,
have h2 : f (finv (f x)) = f x, from inverse_of_exists f default (f x) h1,
show finv (f x) = x, from injf h2

16.5. Lean 中的函数和集合

16.5. Functions and Sets in Lean

在7.4 节中,我们了解了在形式化短语(如“每个大于 2 的素数都是奇数”和“某个素数是偶数”)时如何表示相对化的全称量词和存在量词。类似地,我们可以将语句相对化为集合。在符号逻辑中,表达式xAP(x)缩写x(xAP(x)), 和xAP(x)缩写x(xAP(x))

In Section 7.4 we saw how to represent relativized universal and existential quantifiers when formalizing phrases like “every prime number greater than two is odd” and “some prime number is even.” In a similar way, we can relativize statements to sets. In symbolic logic, the expression xAP(x) abbreviates x(xAP(x)), and xAP(x) abbreviates x(xAP(x)).

Lean 还定义了相对量词的符号:

Lean also defines notation for relativized quantifiers:

variables (X : Type) (A : set X) (P : X  Prop)

#check  x  A, P x
#check  x  A, P x
variables (X : Type) (A : set X) (P : X  Prop)

#check  x  A, P x
#check  x  A, P x

下面是如何使用有界通用量词的一个例子:

Here is an example of how to use the bounded universal quantifier:

example (h :  x  A, P x) (x : X) (h1 : x  A) : P x := h x h1
example (h :  x  A, P x) (x : X) (h1 : x  A) : P x := h x h1

使用有界量词,我们可以讨论函数在特定集合上的行为:

Using bounded quantifiers, we can talk about the behavior of functions on particular sets:

import data.set
open set function

variables {X Y : Type}
variables (A  : set X) (B : set Y)

def maps_to (f : X  Y) (A : set X) (B : set Y) :=
   {x}, x  A  f x  B

def inj_on (f : X  Y) (A : set X) :=
   {x₁ x₂}, x₁  A  x₂  A  f x₁ = f x₂  x₁ = x₂

def surj_on (f : X  Y) (A : set X) (B : set Y) := B  f '' A
import data.set
open set function

variables {X Y : Type}
variables (A  : set X) (B : set Y)

def maps_to (f : X  Y) (A : set X) (B : set Y) :=
   {x}, x  A  f x  B

def inj_on (f : X  Y) (A : set X) :=
   {x₁ x₂}, x₁  A  x₂  A  f x₁ = f x₂  x₁ = x₂

def surj_on (f : X  Y) (A : set X) (B : set Y) := B  f '' A

表达式断言将集合的元素映射到集合,表达式断言是 上的单射。表达式断言,如果将 视为定义在 元素上的函数,则 函数是 上的单射。以下是如何使用它们的示例:maps_to f A BfABinj_on f AfAsurj_on f A BAfB

The expression maps_to f A B asserts that f maps elements of the set A to the set B, and the expression inj_on f A asserts that f is injective on A. The expression surj_on f A B asserts that, viewed as a function defined on elements of A, the function f is surjective onto the set B. Here are examples of how they can be used:

variables (f : X  Y) (A : set X) (B : set Y)

example (h : maps_to f A B) (x : X) (h1 : x  A) : f x  B := h h1

example (h : inj_on f A) (x₁ x₂ : X) (h1 : x₁  A) (h2 : x₂  A)
    (h3 : f x₁ = f x₂) : x₁ = x₂ :=
h h1 h2 h3
variables (f : X  Y) (A : set X) (B : set Y)

example (h : maps_to f A B) (x : X) (h1 : x  A) : f x  B := h h1

example (h : inj_on f A) (x₁ x₂ : X) (h1 : x₁  A) (h2 : x₂  A)
    (h3 : f x₁ = f x₂) : x₁ = x₂ :=
h h1 h2 h3

在下面的例子中,我们将使用带有隐式参数的版本。表达式断言,如果将其视为定义在 元素上的函数,则该函数对集合 是全射。surj_on f A BAfB

In the examples below, we’ll use the versions with implicit arguments. The expression surj_on f A B asserts that, viewed as a function defined on elements of A, the function f is surjective onto the set B.

有了这些概念,我们就可以证明单射函数的复合是单射。证明与上面的类似,但现在我们必须更加小心地将对A和的断言相对化B

With these notions in hand, we can prove that the composition of injective functions is injective. The proof is similar to the one above, though now we have to be more careful to relativize claims to A and B:

theorem inj_on_comp (fAB : maps_to f A B) (hg : inj_on g B) (hf: inj_on f A) :
  inj_on (g  f) A :=
assume x1 : X,
assume x1A : x1  A,
assume x2 : X,
assume x2A : x2  A,
have fx1B : f x1  B, from fAB x1A,
have fx2B : f x2  B, from fAB x2A,
assume h1 : g (f x1) = g (f x2),
have h2 : f x1 = f x2, from hg fx1B fx2B h1,
show x1 = x2, from hf x1A x2A h2
theorem inj_on_comp (fAB : maps_to f A B) (hg : inj_on g B) (hf: inj_on f A) :
  inj_on (g  f) A :=
assume x1 : X,
assume x1A : x1  A,
assume x2 : X,
assume x2A : x2  A,
have fx1B : f x1  B, from fAB x1A,
have fx2B : f x2  B, from fAB x2A,
assume h1 : g (f x1) = g (f x2),
have h2 : f x1 = f x2, from hg fx1B fx2B h1,
show x1 = x2, from hf x1A x2A h2

类似地,我们可以证明全射函数的复合是全射:

We can similarly prove that the composition of surjective functions is surjective:

theorem surj_on_comp (hg : surj_on g B C) (hf: surj_on f A B) :
  surj_on (g  f) A C :=
assume z,
assume zc : z  C,
exists.elim (hg zc) $
assume y (h1 : y  B  g y = z),
exists.elim (hf (and.left h1)) $
assume x (h2 : x  A  f x = y),
show x, x  A  g (f x) = z, from
  exists.intro x
    (and.intro
      (and.left h2)
      (calc
        g (f x) = g y : by rw and.right h2
            ... = z   : by rw and.right h1))
theorem surj_on_comp (hg : surj_on g B C) (hf: surj_on f A B) :
  surj_on (g  f) A C :=
assume z,
assume zc : z  C,
exists.elim (hg zc) $
assume y (h1 : y  B  g y = z),
exists.elim (hf (and.left h1)) $
assume x (h2 : x  A  f x = y),
show x, x  A  g (f x) = z, from
  exists.intro x
    (and.intro
      (and.left h2)
      (calc
        g (f x) = g y : by rw and.right h2
            ... = z   : by rw and.right h1))

下面显示并集的图像是图像的并集:

The following shows that the image of a union is the union of images:

theorem image_union : f '' (A₁  A₂) =f '' A₁  f '' A₂ :=
ext (assume y, iff.intro
  (assume h : y  image f (A₁  A₂),
    exists.elim h $
    assume x h1,
    have xA₁A₂ : x  A₁  A₂, from h1.left,
    have fxy : f x = y, from h1.right,
    or.elim xA₁A₂
      (assume xA₁, or.inl x, xA₁, fxy⟩)
      (assume xA₂, or.inr x, xA₂, fxy⟩))
  (assume h : y  image f A₁  image f A₂,
    or.elim h
      (assume yifA₁ : y  image f A₁,
        exists.elim yifA₁ $
        assume x h1,
        have xA₁ : x  A₁, from h1.left,
        have fxy : f x = y, from h1.right,
        x, or.inl xA₁, fxy⟩)
      (assume yifA₂ : y  image f A₂,
        exists.elim yifA₂ $
        assume x h1,
        have xA₂ : x  A₂, from h1.left,
        have fxy : f x = y, from h1.right,
        x, (or.inr xA₂), fxy⟩)))
theorem image_union : f '' (A₁  A₂) =f '' A₁  f '' A₂ :=
ext (assume y, iff.intro
  (assume h : y  image f (A₁  A₂),
    exists.elim h $
    assume x h1,
    have xA₁A₂ : x  A₁  A₂, from h1.left,
    have fxy : f x = y, from h1.right,
    or.elim xA₁A₂
      (assume xA₁, or.inl x, xA₁, fxy⟩)
      (assume xA₂, or.inr x, xA₂, fxy⟩))
  (assume h : y  image f A₁  image f A₂,
    or.elim h
      (assume yifA₁ : y  image f A₁,
        exists.elim yifA₁ $
        assume x h1,
        have xA₁ : x  A₁, from h1.left,
        have fxy : f x = y, from h1.right,
        x, or.inl xA₁, fxy⟩)
      (assume yifA₂ : y  image f A₂,
        exists.elim yifA₂ $
        assume x h1,
        have xA₂ : x  A₂, from h1.left,
        have fxy : f x = y, from h1.right,
        x, (or.inr xA₂), fxy⟩)))

请注意,表达式扩展为。因此,我们需要提供三条信息: 的值、 的证明和 的证明。在此证明的第八行, 之后,我们可以通过在括号中写入来证明必要的信息。但在这种情况下,Lean 的“匿名构造函数”符号,即用和输入的角括号,允许我们使用更紧凑的表达式。y image f A₁ x, x A₁ f x = yxx A₁f x = yor.inlexists.intro x (and.intro xA₁ fxy)\<\>⟨x, xA₁, fxy⟩

Note that the expression y image f A₁ expands to x, x A₁ f x = y. We therefore need to provide three pieces of information: a value of x, a proof that x A₁, and a proof that f x = y. On the eighth line of this proof, after or.inl, we could justify the necessary information by writing exists.intro x (and.intro xA₁ fxy) in parentheses. But in this case Lean’s “anonymous constructor” notation, that is, the corner brackets entered with \< and \>, allow us to use the more compact expression ⟨x, xA₁, fxy⟩.

16.6.练习

16.6. Exercises

  1. 填写sorry下面最后三个证明中的 。

    import data.set data.int.basic
    open function int
    
    def f (x : ) :  := x + 3
    def g (x : ) :  := -x
    def h (x : ) :  := 2 * x + 3
    
    example : injective f :=
    assume x1 x2,
    assume h1 : x1 + 3 = x2 + 3,   -- Lean knows this is the same as f x1 = f x2
    show x1 = x2, from add_right_cancel h1
    
    example : surjective f :=
    assume y,
    have h1 : f (y - 3) = y, from calc
      f (y - 3) = (y - 3) + 3 : rfl
            ... = y           : by rw sub_add_cancel,
    show  x, f x = y, from exists.intro (y - 3) h1
    
    example (x y : ) (h : 2 * x = 2 * y) : x = y :=
    have h1 : 2  (0 : ), from dec_trivial,  -- this tells Lean to figure it out itself
    show x = y, from mul_left_cancel' h1 h
    
    example (x : ) : -(-x) = x := neg_neg x
    
    example (A B : Type) (u : A  B) (v : B  A) (h : left_inverse u v) :
       x, u (v x) = x :=
    h
    
    example (A B : Type) (u : A  B) (v : B  A) (h : left_inverse u v) :
      right_inverse v u :=
    h
    
    -- fill in the sorry's in the following proofs
    
    example : injective h :=
    sorry
    
    example : surjective g :=
    sorry
    
    example (A B : Type) (u : A  B) (v1 : B  A) (v2 : B  A)
      (h1 : left_inverse v1 u) (h2 : right_inverse v2 u) : v1 = v2 :=
    funext
      (assume x,
        calc
          v1 x = v1 (u (v2 x)) : sorry
           ... = v2 x          : sorry)
    
  2. Fill in the sorry’s in the last three proofs below.

    import data.set data.int.basic
    open function int
    
    def f (x : ) :  := x + 3
    def g (x : ) :  := -x
    def h (x : ) :  := 2 * x + 3
    
    example : injective f :=
    assume x1 x2,
    assume h1 : x1 + 3 = x2 + 3,   -- Lean knows this is the same as f x1 = f x2
    show x1 = x2, from add_right_cancel h1
    
    example : surjective f :=
    assume y,
    have h1 : f (y - 3) = y, from calc
      f (y - 3) = (y - 3) + 3 : rfl
            ... = y           : by rw sub_add_cancel,
    show  x, f x = y, from exists.intro (y - 3) h1
    
    example (x y : ) (h : 2 * x = 2 * y) : x = y :=
    have h1 : 2  (0 : ), from dec_trivial,  -- this tells Lean to figure it out itself
    show x = y, from mul_left_cancel' h1 h
    
    example (x : ) : -(-x) = x := neg_neg x
    
    example (A B : Type) (u : A  B) (v : B  A) (h : left_inverse u v) :
       x, u (v x) = x :=
    h
    
    example (A B : Type) (u : A  B) (v : B  A) (h : left_inverse u v) :
      right_inverse v u :=
    h
    
    -- fill in the sorry's in the following proofs
    
    example : injective h :=
    sorry
    
    example : surjective g :=
    sorry
    
    example (A B : Type) (u : A  B) (v1 : B  A) (v2 : B  A)
      (h1 : left_inverse v1 u) (h2 : right_inverse v2 u) : v1 = v2 :=
    funext
      (assume x,
        calc
          v1 x = v1 (u (v2 x)) : sorry
           ... = v2 x          : sorry)
    
  3. 填写sorry下面的证明。

    import data.set
    open function set
    
    variables {X Y : Type}
    variable  f : X  Y
    variables A B : set X
    
    example : f '' (A  B) = f '' A  f '' B :=
    eq_of_subset_of_subset
      (assume y,
        assume h1 : y  f '' (A  B),
        exists.elim h1 $
        assume x h,
        have h2 : x  A  B, from h.left,
        have h3 : f x = y, from h.right,
        or.elim h2
          (assume h4 : x  A,
            have h5 : y  f '' A, from x, h4, h3⟩,
            show y  f '' A  f '' B, from or.inl h5)
          (assume h4 : x  B,
            have h5 : y  f ''  B, from x, h4, h3⟩,
            show y  f '' A  f '' B, from or.inr h5))
      (assume y,
        assume h2 : y  f '' A  f '' B,
        or.elim h2
          (assume h3 : y  f '' A,
            exists.elim h3 $
            assume x h,
            have h4 : x  A, from h.left,
            have h5 : f x = y, from h.right,
            have h6 : x  A  B, from or.inl h4,
            show y  f '' (A  B), from x, h6, h5⟩)
          (assume h3 : y  f '' B,
            exists.elim h3 $
            assume x h,
            have h4 : x  B, from h.left,
            have h5 : f x = y, from h.right,
            have h6 : x  A  B, from or.inr h4,
            show y  f '' (A  B), from x, h6, h5⟩))
    
    -- remember, x ∈ A ∩ B is the same as x ∈ A ∧ x ∈ B
    example (x : X) (h1 : x  A) (h2 : x  B) : x  A  B :=
    and.intro h1 h2
    
    example (x : X) (h1 : x  A  B) : x  A :=
    and.left h1
    
    -- Fill in the proof below.
    -- (It should take about 8 lines.)
    
    example : f '' (A  B)  f '' A  f '' B :=
    assume y,
    assume h1 : y  f '' (A  B),
    show y  f '' A  f '' B, from sorry
    
  4. Fill in the sorry in the proof below.

    import data.set
    open function set
    
    variables {X Y : Type}
    variable  f : X  Y
    variables A B : set X
    
    example : f '' (A  B) = f '' A  f '' B :=
    eq_of_subset_of_subset
      (assume y,
        assume h1 : y  f '' (A  B),
        exists.elim h1 $
        assume x h,
        have h2 : x  A  B, from h.left,
        have h3 : f x = y, from h.right,
        or.elim h2
          (assume h4 : x  A,
            have h5 : y  f '' A, from x, h4, h3⟩,
            show y  f '' A  f '' B, from or.inl h5)
          (assume h4 : x  B,
            have h5 : y  f ''  B, from x, h4, h3⟩,
            show y  f '' A  f '' B, from or.inr h5))
      (assume y,
        assume h2 : y  f '' A  f '' B,
        or.elim h2
          (assume h3 : y  f '' A,
            exists.elim h3 $
            assume x h,
            have h4 : x  A, from h.left,
            have h5 : f x = y, from h.right,
            have h6 : x  A  B, from or.inl h4,
            show y  f '' (A  B), from x, h6, h5⟩)
          (assume h3 : y  f '' B,
            exists.elim h3 $
            assume x h,
            have h4 : x  B, from h.left,
            have h5 : f x = y, from h.right,
            have h6 : x  A  B, from or.inr h4,
            show y  f '' (A  B), from x, h6, h5⟩))
    
    -- remember, x ∈ A ∩ B is the same as x ∈ A ∧ x ∈ B
    example (x : X) (h1 : x  A) (h2 : x  B) : x  A  B :=
    and.intro h1 h2
    
    example (x : X) (h1 : x  A  B) : x  A :=
    and.left h1
    
    -- Fill in the proof below.
    -- (It should take about 8 lines.)
    
    example : f '' (A  B)  f '' A  f '' B :=
    assume y,
    assume h1 : y  f '' (A  B),
    show y  f '' A  f '' B, from sorry
    

17.自然数与归纳法

17. The Natural Numbers and Induction

本章标志着从抽象到具体的过渡。从集合、关系和函数的角度看待数学世界,为我们提供了思考数学对象和结构及其之间关系的有用方法。然而,在某个时候,我们需要开始思考特定的数学对象和结构,而自然数是一个很好的起点。19 世纪数学家利奥波德·克罗内克曾宣称“上帝创造了整数;其他一切都是人类的杰作。”他这样说的意思是,自然数(以及整数,我们也将在下面讨论)是数学世界的基本组成部分,许多其他感兴趣的对象和结构都可以从这些对象和结构中构建出来。

This chapter marks a transition from the abstract to the concrete. Viewing the mathematical universe in terms of sets, relations, and functions gives us useful ways of thinking about mathematical objects and structures and the relationships between them. At some point, however, we need to start thinking about particular mathematical objects and structures, and the natural numbers are a good place to start. The nineteenth century mathematician Leopold Kronecker once proclaimed “God created the whole numbers; everything else is the work of man.” By this he meant that the natural numbers (and the integers, which we will also discuss below) are a fundamental component of the mathematical universe, and that many other objects and structures of interest can be constructed from these.

在本章中,我们将考虑自然数及其基本原理。在第 18 章中,我们将看到,即使是加法和乘法等基本运算也可以使用此处描述的方法进行定义,并且它们的属性可以从这些基本原理中推导出来。不过,我们在本章中的介绍将保持非正式。在第 19 章中,我们将看到这些原理如何在数论中发挥作用,数论是数学中最古老、最受尊敬的分支之一。

In this chapter, we will consider the natural numbers and the basic principles that govern them. In Chapter 18 we will see that even basic operations like addition and multiplication can be defined using means described here, and their properties derived from these basic principles. Our presentation in this chapter will remain informal, however. In Chapter 19, we will see how these principles play out in number theory, one of the oldest and most venerable branches of mathematics.

17.1.归纳原理

17.1. The Principle of Induction

自然数集是

The set of natural numbers is the set

N={0,1,2,3,}.

过去,人们对于自然数集应该以 0 还是 1 开头有不同的看法,但如今大多数数学家都认为自然数集以 0 开头。逻辑学家通常将函数称为s(n)=n+1后继函数,因为它映射每个自然数,n,到它后面的那个。自然数的特殊之处在于它们是由数字零和后继函数生成的,也就是说,构造自然数的唯一方法是从0并有限次地应用后继函数。从基础的角度来看,我们在这里有陷入循环的危险,因为我们不清楚如何在不谈论自然数本身的情况下解释“有限次”应用函数的含义。但以下原理,即所谓的归纳原理,以非循环的方式描述了自然数的这一基本属性。

In the past, opinions have differed as to whether the set of natural numbers should start with 0 or 1, but these days most mathematicians take them to start with 0. Logicians often call the function s(n)=n+1 the successor function, since it maps each natural number, n, to the one that follows it. What makes the natural numbers special is that they are generated by the number zero and the successor function, which is to say, the only way to construct a natural number is to start with 0 and apply the successor function finitely many times. From a foundational standpoint, we are in danger of running into a circularity here, because it is not clear how we can explain what it means to apply a function “finitely many times” without talking about the natural numbers themselves. But the following principle, known as the principle of induction, describes this essential property of the natural numbers in a non-circular way.


归纳原理。P是自然数的任何属性。假设P成立,并且每当P成立的自然数n,那么它的后继也成立,n+1。 然后P对每个自然数都成立。

Principle of Induction. Let P be any property of natural numbers. Suppose P holds of zero, and whenever P holds of a natural number n, then it holds of its successor, n+1. Then P holds of every natural number.


这反映了自然数由零和后继运算生成的图像:通过覆盖零和后继的情况,我们可以处理所有自然数。

This reflects the image of the natural numbers as being generated by zero and the successor operation: by covering the zero and successor cases, we take care of all the natural numbers.

归纳原理提供了一种证明每个自然数都有某种属性的方法:证明P对每个自然数都成立,证明0并证明每当它​​成立时n,它成立n+1。这种证明形式称为归纳证明。第一个必需任务称为基本情况,第二个必需任务称为归纳步骤。归纳步骤需要暂时固定一个自然数n,假设P持有n,然后证明P持有n+1. 在此背景下,假设P持有n被称为归纳假设

The principle of induction provides a recipe for proving that every natural number has a certain property: to show that P holds of every natural number, show that it holds of 0, and show that whenever it holds of some number n, it holds of n+1. This form of proof is called a proof by induction. The first required task is called the base case, and the second required task is called the induction step. The induction step requires temporarily fixing a natural number n, assuming that P holds of n, and then showing that P holds of n+1. In this context, the assumption that P holds of n is called the inductive hypothesis.

你可以将归纳证明想象成一种一次性推倒无数张多米诺骨牌的方法。我们设置好机制,推倒多米诺骨牌 0(基本情况),每张多米诺骨牌都会推倒下一张多米诺骨牌(归纳步骤)。因此,多米诺骨牌 0 会推倒多米诺骨牌 1;多米诺骨牌 1 会推倒多米诺骨牌 2,依此类推。

You can visualize proof by induction as a method of knocking down an infinite stream of dominoes, all at once. We set the mechanism in place and knock down domino 0 (the base case), and every domino knocks down the next domino (the induction step). So domino 0 knocks down domino 1; that knocks down domino 2, and so on.

这是一个归纳证明的例子。

Here is an example of a proof by induction.


定理。对于每个自然数n

Theorem. For every natural number n,

1+2++2n=2n+11.

证明。我们通过归纳法证明n. 在基本情况下,当n=0,我们有1=20+11,按要求。

Proof. We prove this by induction on n. In the base case, when n=0, we have 1=20+11, as required.

对于诱导步骤,修复n,并作出归纳假设

For the induction step, fix n, and assume the inductive hypothesis

1+2++2n=2n+11.

我们需要证明这一说法同样适用于n替换为n+1。但这只是一个计算:

We need to show that this same claim holds with n replaced by n+1. But this is just a calculation:

1+2++2n+1=(1+2++2n)+2n+1=2n+11+2n+1=22n+11=2n+21.

在一阶逻辑符号中,如果我们写P(n)意思是P持有n,我们可以将归纳原理表达如下:

In the notation of first-order logic, if we write P(n) to mean that P holds of n, we could express the principle of induction as follows:

P(0)n(P(n)P(n+1))nP(n).

但请注意,归纳原理表明,公理适用于每个属性 P,这意味着我们也应该正确地使用全称量词:

But notice that the principle of induction says that the axiom holds for every property P, which means that we should properly use a universal quantifier for that, too:

P(P(0)n(P(n)P(n+1))nP(n)).

对属性进行量化使我们超出了一阶逻辑的范畴;因此,归纳推理是二阶原理。

Quantifying over properties takes us out of the realm of first-order logic; induction is therefore a second-order principle.

归纳证明的模式可以通过以下自然演绎规则更自然地表达:

The pattern for a proof by induction is expressed even more naturally by the following natural deduction rule:

你应该思考如何用自然演绎来正式表示本章中的某些证明。

You should think about how some of the proofs in this chapter could be represented formally using natural deduction.

另一个归纳证明的例子是,我们推导出一个公式,给定任意有限集S,确定子集的数量S例如,二元素集合有四个子集{1,2},即{1}{2}, 和{1,2}你应该确信这个集合有八个子集{1,2,3}.下面的定理建立了一般模式。

For another example of a proof by induction, let us derive a formula that, given any finite set S, determines the number of subsets of S. For example, there are four subsets of the two-element set {1,2}, namely , {1}, {2}, and {1,2}. You should convince yourself that there are eight subsets of the set {1,2,3}. The following theorem establishes the general pattern.


定理。对于任何有限集S, 如果Sn元素,则有2n的子集S

Theorem. For any finite set S, if S has n elements, then there are 2n subsets of S.

证明。我们利用归纳法n。在基本情况下,只有一个集合0元素、空集,并且恰好有一个空集子集,正如所要求的。

Proof. We use induction on n. In the base case, there is only one set with 0 elements, the empty set, and there is exactly one subset of the empty set, as required.

在归纳情况下,假设Sn+1元素。让a是任意元素S,并让S是包含剩余的n元素。为了计算S,我们把它们分成两组。

In the inductive case, suppose S has n+1 elements. Let a be any element of S, and let S be the set containing the remaining n elements. In order to count the subsets of S, we divide them into two groups.

首先,我们考虑S不包含a. 这些正是S,根据归纳假设,有2n其中。

First, we consider the subsets of S that don’t contain a. These are exactly the subsets of S, and by the inductive hypothesis, there are 2n of those.

接下来我们考虑S确实含有a. 每一个都是通过选择一个子集获得的S并添加a. 由于有2n的子集S, 有2n的子集S包含a

Next we consider the subsets of S that do contain a. Each of these is obtained by choosing a subset of S and adding a. Since there are 2n subsets of S, there are 2n subsets of S that contain a.

综合起来,有2n+2n=2n+1的子集S,按要求。

Taken together, then, there are 2n+2n=2n+1 subsets of S, as required.


我们已经看到,域的属性与域的子集之间存在对应关系。对于每个属性P自然数,我们可以考虑集合S具有该属性的自然数,对于每个自然数集,我们都可以考虑属于该集合的属性。例如,我们可以讨论偶数属性,或者讨论偶数集。在这种对应关系下,归纳原理可以表述如下:

We have seen that there is a correspondence between properties of a domain and subsets of a domain. For every property P of natural numbers, we can consider the set S of natural numbers with that property, and for every set of natural numbers, we can consider the property of being in that set. For example, we can talk about the property of being even, or talk about the set of even numbers. Under this correspondence, the principle of induction can be cast as follows:


归纳原理。S是包含以下项的任何自然数集0并在后继操作下关闭。然后S=N

Principle of Induction. Let S be any set of natural numbers that contains 0 and is closed under the successor operation. Then S=N.


在这里,这么说S是“在后继操作下封闭的”,这意味着每当一个数字n位于S,所以n+1

Here, saying that S is “closed under the successor operation” means that whenever a number n is in S, so is n+1.

17.2.归纳法的变体

17.2. Variants of Induction

在本节中,我们将讨论归纳原理中经常用到的变体。重要的是要认识到,这些变体都可以使用上一节中所述的归纳原理来证明,因此不必将它们视为基本原理。

In this section, we will consider variations on the principle of induction that are often useful. It is important to recognize that each of these can be justified using the principle of induction as stated in the last section, so they need not be taken as fundamental.

第一个没什么了不起的:而不是从0,我们可以从任意自然数开始,m

The first one is no great shakes: instead of starting from 0, we can start from any natural number, m.


从起点归纳原理。P是自然数的任何属性,并设m是任意自然数。假设P持有m,并且每当P成立的自然数n大于或等于m,那么它的后继也成立,n+1。 然后P成立每个大于或等于的自然数m

Principle of Induction from a Starting Point. Let P be any property of natural numbers, and let m be any natural number. Suppose P holds of m, and whenever P holds of a natural number n greater than or equal to m, then it holds of its successor, n+1. Then P holds of every natural number greater than or equal to m.


假设最后这条原则的假设,如果我们让P(n)是财产“P持有m+n”我们可以证明P持有每一个n按照普通的归纳原理。但这意味着P大于或等于的每个数字都成立m

Assuming the hypotheses of this last principle, if we let P(n) be the property “P holds of m+n,” we can prove that P holds of every n by the ordinary principle of induction. But this means that P holds of every number greater than or equal to m.

下面是使用此种归纳法变体的证明的一个例子。

Here is one example of a proof using this variant of induction.


定理。对于每个自然数n52n>n2

Theorem. For every natural number n5, 2n>n2.

证明。通过归纳n。 什么时候n=5,我们有2n=32>25=n2,按要求。

Proof. By induction on n. When n=5, we have 2n=32>25=n2, as required.

对于诱导步骤,假设n52n>n2。 自从n大于或等于5,我们有2n+13nn2, 所以

For the induction step, suppose n5 and 2n>n2. Since n is greater than or equal to 5, we have 2n+13nn2, and so

(n+1)2=n2+2n+1n2+n2<2n+2n=2n+1.

再举一个例子,让我们推导出凸多边形内角和的公式。如果两个顶点之间的每条线都在多边形内,则称该多边形为凸多边形。我们将无需证明就接受这个显而易见的事实:任何边多于三条的凸多边形都可以细分为三角形和边少一条的凸多边形,即通过封闭任意两条连续边形成三角形。我们还会无需证明就接受这个基本的几何事实:任何三角形的内角和都是 180 度。

For another example, let us derive a formula for the sum total of the angles in a convex polygon. A polygon is said to be convex if every line between two vertices stays inside the polygon. We will accept without proof the visually obvious fact that one can subdivide any convex polygon with more than three sides into a triangle and a convex polygon with one fewer side, namely, by closing off any two consecutive sides to form a triangle. We will also accept, without proof, the basic geometric fact that the sum of the angles of any triangle is 180 degrees.


定理。对于任意n3,任意凸面的角度之和n-gon 是180(n2)

Theorem. For any n3, the sum of the angles of any convex n-gon is 180(n2).

证明。在基本情况下,当n=3,这可以归结为任何三角形的角度和都是 180 度。

Proof. In the base case, when n=3, this reduces to the statement that the sum of the angles in any triangle is 180 degrees.

对于诱导步骤,假设n3,并让P(n+1)-gon.除法P变成一个三角形和一个n边形。根据归纳假设,n-gon 是180(n2)度,三角形的角度总和为180度。这些角度的测量值加在一起构成了P,总计180(n2)+180=180(n1)度。

For the induction step, suppose n3, and let P be a convex (n+1)-gon. Divide P into a triangle and an n-gon. By the inductive hypotheses, the sum of the angles of the n-gon is 180(n2) degrees, and the sum of the angles of the triangle is 180 degrees. The measures of these angles taken together make up the sum of the measures of the angles of P, for a total of 180(n2)+180=180(n1) degrees.


对于我们的第二个例子,我们将考虑完全诱导原理,有时也称为完全诱导

For our second example, we will consider the principle of complete induction, also sometimes known as total induction.


完全归纳原理。P满足以下条件的任何属性:对于任何自然数n,无论何时P小于的每个数字都成立n,它也成立n。 然后P对每个自然数都成立。

Principle of Complete Induction. Let P be any property that satisfies the following: for any natural number n, whenever P holds of every number less than n, it also holds of n. Then P holds of every natural number.


请注意,没有必要为零单独列出特殊情况:对于任何属性PP对所有小于零的自然数都成立,原因很简单,因为没有这样的自然数!因此,任何这样的属性都会自动对零成立。

Notice that there is no need to break out a special case for zero: for any property P, P holds of all the natural numbers less than zero, for the trivial reason that there aren’t any! So, in particular, any such property automatically holds of zero.

还要注意,如果这样的属性P小于的每个数字都成立n,那么它也适用于小于n+1(为什么?)。那么,对于这样一个P,普通的归纳原理意味着对于每个自然数nP成立小于的每个自然数n。但这只是一种迂回的说法P对每个自然数都成立。换句话说,我们用普通的归纳法证明了完全归纳原理的合理性。

Notice also that if such a property P holds of every number less than n, then it also holds of every number less than n+1 (why?). So, for such a P, the ordinary principle of induction implies that for every natural number n, P holds of every natural number less than n. But this is just a roundabout way of saying that P holds of every natural number. In other words, we have justified the principle of complete induction using ordinary induction.

要使用完全归纳原理,我们只需要让n是任意自然数,并证明P持有n,假设它对每个较小的数字都成立。将其与普通的归纳原理进行比较,后者要求我们证明P(n+1)仅假设P(n)以下运用该原则的例子逐字逐句地摘自本书的引言:

To use the principle of complete induction we merely have to let n be any natural number and show that P holds of n, assuming that it holds of every smaller number. Compare this to the ordinary principle of induction, which requires us to show P(n+1) assuming only P(n). The following example of the use of this principle is taken verbatim from the introduction to this book:


定理。每个大于或等于 2 的自然数都可以写成素数的乘积。

Theorem. Every natural number greater than or equal to 2 can be written as a product of primes.

证明。我们通过归纳法进行n。 让n为大于 2 的任意自然数。如果n是素数,我们就完成了;我们可以考虑n本身就是一个具有一个因素的产品。否则,n是复合的,我们可以写成n=mk在哪里mk小于n并且大于 1。根据归纳假设,mk可以写成素数的乘积:

Proof. We proceed by induction on n. Let n be any natural number greater than 2. If n is prime, we are done; we can consider n itself as a product with one factor. Otherwise, n is composite, and we can write n=mk where m and k are smaller than n and greater than 1. By the inductive hypothesis, each of m and k can be written as a product of primes:

m=p1p2puk=q1q2qv.

但后来我们

But then we have

n=mk=p1p2puq1q2qv.

我们看到n正如所要求的,是素数的乘积。

We see that n is a product of primes, as required.


最后,我们将考虑归纳法的另一种表述,即最小元素原理。

Finally, we will consider another formulation of induction, known as the least element principle.


最小元素原理。假设P是自然数的一些属性,假设P持有一些n. 那么最小值为n为此P持有。

The Least Element Principle. Suppose P is some property of natural numbers, and suppose P holds of some n. Then there is a smallest value of n for which P holds.


事实上,用经典推理来说,这相当于完​​全归纳原理。要理解这一点,请考虑上述陈述的逆否命题:“如果不存在最小值,P成立,那么P对任何自然数都不成立。”设Q(n)是财产“P不成立n” 表示不存在最小值P成立意味着,对于每一个n, 如果P持有于n,那么它对小于的某个数成立n;这相当于说,对于每一个n, 如果Q不成立n,则存在一个较小的值Q不成立。相当于说如果Q对于每个小于的数字都成立n,它适用于n也一样。同样地,说P对任何自然数都不成立,这相当于说Q适用于每个自然数。换句话说,用最小元素原理的反面代替最小元素原理,用P“不”Q”,我们有完全归纳原理。由于每个陈述都等同于它的逆否命题,并且每个谓词都有其否定版本,因此这两个原理是相同的。

In fact, using classical reasoning, this is equivalent to the principle of complete induction. To see this, consider the contrapositive of the statement above: “if there is no smallest value for which P holds, then P doesn’t hold of any natural number.” Let Q(n) be the property “P does not hold of n.” Saying that there is no smallest value for which P holds means that, for every n, if P holds at n, then it holds of some number smaller than n; and this is equivalent to saying that, for every n, if Q doesn’t hold at n, then there is a smaller value for which Q doesn’t hold. And that is equivalent to saying that if Q holds for every number less than n, it holds for n as well. Similarly, saying that P doesn’t hold of any natural number is equivalent to saying that Q holds of every natural number. In other words, replacing the least element principle by its contrapositive, and replacing P by “not Q,” we have the principle of complete induction. Since every statement is equivalent to its contrapositive, and every predicate has its negated version, the two principles are the same.

因此,最小元素原理的使用方式与完全归纳原理的使用方式非常相似,这并不奇怪。例如,下面是上述证明的表述。请注意,它被表述为矛盾证明。

It is not surprising, then, that the least element principle can be used in much the same way as the principle of complete induction. Here, for example, is a formulation of the previous proof in these terms. Notice that it is phrased as a proof by contradiction.


定理。每个大于等于 2 的自然数都可以写成素数的乘积。

Theorem. Every natural number greater than equal to 2 can be written as a product of primes.

证明。相反,假设某个大于或等于 2 的自然数不能写成素数的乘积。根据最小元素原理,存在一个最小的这样的元素;称之为n。 然后n不是素数,而且由于它大于或等于 2,所以它一定是合数。因此我们可以写成n=mk在哪里mk小于n且大于 1。根据假设n,每个mk可以写成素数的乘积:

Proof. Suppose, to the contrary, some natural number greater than or equal to 2 cannot be written as a product of primes. By the least element principle, there is a smallest such element; call it n. Then n is not prime, and since it is greater than or equal to 2, it must be composite. Hence we can write n=mk where m and k are smaller than n and greater than 1. By the assumption on n, each of m and k can be written as a product of primes:

m=p1p2puk=q1q2qv.

但后来我们

But then we have

n=mk=p1p2puq1q2qv.

我们看到n是素数的乘积,这与以下事实相矛盾:n不能写成素数的乘积。

We see that n is a product of primes, contradicting the fact that n cannot be written as a product of primes.


下面是另一个示例:

Here is another example:


定理。每个自然数都是有趣的。

Theorem. Every natural number is interesting.

证明。相反,假设某个自然数是无趣的。那么有一个最小的自然数,n。 换句话说,n是最小的无趣数。但这真的很有趣!矛盾。

Proof. Suppose, to the contrary, some natural number is uninteresting. Then there is a smallest one, n. In other words, n is the smallest uninteresting number. But that is really interesting! Contradiction.


17.3.递归定义

17.3. Recursive Definitions

假设我告诉你我有一个函数f:NN考虑到,满足以下属性:

Suppose I tell you that I have a function f:NN in mind, satisfying the following properties:

f(0)=1f(n+1)=2f(n)

你能推断出f? 尝试计算几个值:

What can you infer about f? Try calculating a few values:

f(1)=f(0+1)=2f(0)=2f(2)=f(1+1)=2f(1)=4f(3)=f(2+1)=2f(2)=8

很快就会发现,对于每一个nf(n)=2n

It soon becomes apparent that for every n, f(n)=2n.

更有趣的是,上述两个条件指定f也就是说,只有一个函数满足上述要求。事实上,f取自然数中的值;它可以取任何其他域中的值。所需要的只是一个值f(0)以及计算值的方法f(n+1)按照nf(n)递归定义原理表明:

What is more interesting is that the two conditions above specify all the values of f, which is to say, there is exactly one function meeting the specification above. In fact, it does not matter that f takes values in the natural numbers; it could take values in any other domain. All that is needed is a value of f(0) and a way to compute the value of f(n+1) in terms of n and f(n). This is what the principle of definition by recursion asserts:


递归定义原理。设A是任意集合,假设a位于A, 和g:N×AA.然后有一个独特的功能f满足以下两个条款:

Principle of Definition by Recursion. Let A be any set, and suppose a is in A, and g:N×AA. Then there is a unique function f satisfying the following two clauses:

f(0)=af(n+1)=g(n,f(n)).

递归定义原理同时提出了两个主张:首先,存在一个函数f满足上述条件,其次,任何两个函数f1f2满足这些子句的函数是相等的,也就是说,它们对于每个输入都具有相同的值。在本节开头的例子中,A只是Ng(n,f(n))=2f(n)

The principle of recursive definition makes two claims at once: first, that there is a function f satisfying the clauses above, and, second, that any two functions f1 and f2 satisfying those clauses are equal, which is to say, they have the same values for every input. In the example with which we began this section, A is just N and g(n,f(n))=2f(n).

在一些公理框架中,递归定义原理可以用归纳原理来证明。在其他框架中,归纳原理可以看作递归定义原理的一个特例。现在,我们先简单地将两者都视为自然数的基本性质。

In some axiomatic frameworks, the principle of recursive definition can be justified using the principle of induction. In others, the principle of induction can be viewed as a special case of the principle of recursive definition. For now, we will simply take both to be fundamental properties of the natural numbers.

作为递归定义的另一个例子,考虑函数g:NN由以下子句递归定义:

As another example of a recursive definition, consider the function g:NN defined recursively by the following clauses:

g(0)=1g(n+1)=(n+1)g(n)

尝试计算前几个值。解开定义,我们看到g(n)=123(n1)n对于每一个n;事实上,递归定义通常是使使用“…”的表达式精确的正确方法。值g(n)读作“n阶乘”,并写成n!

Try calculating the first few values. Unwrapping the definition, we see that g(n)=123(n1)n for every n; indeed, definition by recursion is usually the proper way to make expressions using “…” precise. The value g(n) is read “n factorial,” and written n!.

事实上,求和符号

Indeed, summation notation

i<nf(i)=f(0)+f(1)++f(n1)

和产品符号

and product notation

i<nf(i)=f(0)f(1)f(n1)

也可以使用递归定义使其精确化。例如,函数k(n)=i<nf(i)可以递归定义如下:

can also be made precise using recursive definitions. For example, the function k(n)=i<nf(i) can be defined recursively as follows:

k(0)=0k(n+1)=k(n)+f(n)

归纳和递归是互补的原理,通常证明递归定义函数的方法是使用归纳原理。例如,以下定理提供了求和公式1+2++n, 按照n

Induction and recursion are complementary principles, and typically the way to prove something about a recursively defined function is to use the principle of induction. For example, the following theorem provides a formulas for the sum 1+2++n, in terms of n.


定理。对于每一个ni<n+1i=n(n+1)/2

Theorem. For every n, i<n+1i=n(n+1)/2.

证明。在基本情况下,当n=0,双方都等于0

Proof. In the base case, when n=0, both sides are equal to 0.

在归纳步骤中,我们有

In the inductive step, we have

i<n+2i=(i<n+1i)+(n+1)=n(n+1)/2+n+1=n2+n2+2n+22=n2+3n+22=(n+1)(n+2)2.

递归定义原理的变体与归纳原理的变体一样多。例如,与完全归纳原理类似,我们可以指定f(n)就价值观而言f在所有输入都小于n。 什么时候n2例如,以下定义指定函数的值fib(n)相对于其前两部作品而言:

There are just as many variations on the principle of recursive definition as there are on the principle of induction. For example, in analogy to the principle of complete induction, we can specify a value of f(n) in terms of the values that f takes at all inputs smaller than n. When n2, for example, the following definition specifies the value of a function fib(n) in terms of its two predecessors:

fib(0)=0fib(1)=1fib(n+2)=fib(n+1)+fib(n)

计算值fib0,1,2,我们得到

Calculating the values of fib on 0,1,2, we obtain

0,1,1,2,3,5,8,13,21,

这里,第二个数字之后的每个连续数字都是它前面两个值的总和。这被称为斐波那契数列,相应的数字称为斐波那契数。普通的数学表示会写成Fn而不是fib(n)并通过以下方程指定序列:

Here, after the second number, each successive number is the sum of the two values preceding it. This is known as the Fibonacci sequence, and the corresponding numbers are known as the Fibonacci numbers. An ordinary mathematical presentation would write Fn instead of fib(n) and specify the sequence with the following equations:

F0=0,F1=1,Fn+2=Fn+1+Fn

但是现在您可以认识到这种规范是对递归定义原理的隐含诉求。我们要求您在下面的练习中证明有关斐波那契数列的一些事实。

But you can now recognize such a specification as an implicit appeal to the principle of definition by recursion. We ask you to prove some facts about the Fibonacci sequence in the exercises below.

17.4.定义算术运算

17.4. Defining Arithmetic Operations

事实上,我们甚至可以使用递归定义原理来定义自然数的最基本运算,并表明它们具有我们期望的属性。从基础的角度来看,我们可以将自然数描述为一个集合,N,具有独特的元素0和一个函数,succ(m)对于每个自然数m,返回其后继。它们满足以下条件:

In fact, we can even use the principle of recursive definition to define the most basic operations on the natural numbers and show that they have the properties we expect them to have. From a foundational standpoint, we can characterize the natural numbers as a set, N, with a distinguished element 0 and a function, succ(m), which, for every natural number m, returns its successor. These satisfy the following:

  • 0succ(m)对于任意mN

  • 0succ(m) for any m in N.

  • 对于每一个mnN, 如果mn, 然后succ(m)succ(n)。 换句话说,succ单射

  • For every m and n in N, if mn, then succ(m)succ(n). In other words, succ is injective.

  • 如果AN具有以下属性0位于A并且无论何时n位于A然后succ(n)位于A, 然后A=N

  • If A is any subset of N with the property that 0 is in A and whenever n is in A then succ(n) is in A, then A=N.

最后一句可以改写为归纳原则:

The last clause can be reformulated as the principle of induction:

认为P(n)是自然数的任何属性,例如P持有0,并且对于每一个nP(n)暗示P(succ(n)).然后每个P对每个自然数都成立。

Suppose P(n) is any property of natural numbers, such that P holds of 0, and for every n, P(n) implies P(succ(n)). Then every P holds of every natural number.

请记住,此原理可用于证明递归定义原理:

Remember that this principle can be used to justify the principle of definition by recursion:

A是任意集合,a是任意元素A,并让g(n,m)是来自的任何函数N×AA.然后有一个独特的功能f:NA满足以下两个条款:

  • f(0)=a

  • f(succ(n))=g(n,f(n))对于每一个nN

Let A be any set, a be any element of A, and let g(n,m) be any function from N×A to A. Then there is a unique function f:NA satisfying the following two clauses:

  • f(0)=a

  • f(succ(n))=g(n,f(n)) for every n in N

我们可以利用递归定义的原理,用以下两个子句来定义加法:

We can use the principle of recursive definition to define addition with the following two clauses:

m+0=mm+succ(n)=succ(m+n)

请注意,我们正在修复m并将其视为n.如果我们写1=succ(0)2=succ(1)等等,很容易证明n+1=succ(n)根据加法的定义。

Note that we are fixing m, and viewing this as a function of n. If we write 1=succ(0), 2=succ(1), and so on, it is easy to prove n+1=succ(n) from the definition of addition.

我们可以使用以下两个子句来定义乘法:

We can proceed to define multiplication using the following two clauses:

m0=0msucc(n)=mn+m

我们还可以通过以下方式定义前驱函数

We can also define a predecessor function by

pred(0)=0pred(succ(n))=n

我们可以通过以下方式定义截断减法

We can define truncated subtraction by

m˙0=mm˙(succ(n))=pred(m˙n)

利用这些定义和归纳原理,可以证明以下所有恒等式:

With these definitions and the induction principle, one can prove all the following identities:

  • n0暗示succ(pred(n))=n

  • n0 implies succ(pred(n))=n

  • 0+n=n

  • 0+n=n

  • succ(m)+n=succ(m+n)

  • succ(m)+n=succ(m+n)

  • (m+n)+k=m+(n+k)

  • (m+n)+k=m+(n+k)

  • m+n=n+m

  • m+n=n+m

  • m(n+k)=mn+mk

  • m(n+k)=mn+mk

  • 0n=0

  • 0n=0

  • 1n=n

  • 1n=n

  • (mn)k=m(nk)

  • (mn)k=m(nk)

  • mn=nm

  • mn=nm

我们将在这里完成前五个,并将剩下的作为练习。

We will do the first five here, and leave the remaining ones as exercises.


命题。对于每个自然数n, 如果n0然后succ(pred(n))=n

Proposition. For every natural number n, if n0 then succ(pred(n))=n.

证明。通过归纳n。我们排除了以下情况n0,所以我们只需要证明该声明适用于succ(n)。但在这种情况下,我们有succ(pred(succ(n))=succ(n)通过前置函数的第二个定义子句。

Proof. By induction on n. We have ruled out the case where n is 0, so we only need to show that the claim holds for succ(n). But in that case, we have succ(pred(succ(n))=succ(n) by the second defining clause of the predecessor function.

命题。对于每一个n0+n=n

Proposition. For every n, 0+n=n.

证明。通过归纳n.我们有0+0=0通过第一个定义子句来添加。假设0+n=n,我们有0+succ(n)=succ(0+n)=n,使用第二个定义子句进行添加。

Proof. By induction on n. We have 0+0=0 by the first defining clause for addition. And assuming 0+n=n, we have 0+succ(n)=succ(0+n)=n, using the second defining clause for addition.

命题。对于每一个mnsucc(m)+n=succ(m+n)

Proposition. For every m and n, succ(m)+n=succ(m+n).

证明。修复m并使用归纳法n。 然后n=0,我们有succ(m)+0=succ(m)=succ(m+0),使用第一个定义子句进行补充。假设断言适用于n,我们有

Proof. Fix m and use induction on n. Then n=0, we have succ(m)+0=succ(m)=succ(m+0), using the first defining clause for addition. Assuming the claim holds for n, we have

succ(m)+succ(n)=succ(succ(m)+n)=succ(succ(m+n))=succ(m+succ(n))

使用归纳假设和第二个定义子句进行加法。

using the inductive hypothesis and the second defining clause for addition.

命题。对于每一个mn, 和k(m+n)+k=m+(n+k)

Proposition. For every m, n, and k, (m+n)+k=m+(n+k).

证明。通过归纳k. 这种情况k=0很容易,在诱导步骤中我们有

Proof. By induction on k. The case where k=0 is easy, and in the induction step we have

(m+n)+succ(k)=succ((m+n)+k)=succ(m+(n+k))=m+succ(n+k)=m+(n+succ(k)))

使用归纳假设和加法的定义。

using the inductive hypothesis and the definition of addition.

命题。对于每一对自然数mnm+n=n+m

Proposition. For every pair of natural numbers m and n, m+n=n+m.

证明。通过归纳n。使用上面的第二个命题,基本情况很容易。在归纳步骤中,我们有

Proof. By induction on n. The base case is easy using the second proposition above. In the inductive step, we have

m+succ(n)=succ(m+n)=succ(n+m)=succ(n)+m

使用上面的第三个命题。

using the third proposition above.


17.5.自然数的算术

17.5. Arithmetic on the Natural Numbers

继续上一节的内容,我们可以确定在日常数学中发挥作用的自然数的所有基本属性。我们在这里总结了主要内容:

Continuing as in the last section, we can establish all the basic properties of the natural numbers that play a role in day-to-day mathematics. We summarize the main ones here:

m+n=n+m(commutativity of addition)m+(n+k)=(m+n)+k(associativity of addition)n+0=n(0 is a neutral element for addition)nm=mn(commutativity of multiplication)m(nk)=(mn)k(associativity of multiplication)n1=n(1 is an neutral element for multiplication)n(m+k)=nm+nk(distributivity)n0=0(0 is an absorbing element for multiplication)

在普通的数学论证或计算中,它们可以在没有明确理由的情况下使用。我们还具有以下属性:

In an ordinary mathematical argument or calculation, they can be used without explicit justification. We also have the following properties:

  • n+10

  • n+10

  • 如果n+k=m+k然后n=m

  • if n+k=m+k then n=m

  • 如果nk=mkk0然后n=m

  • if nk=mk and k0 then n=m

我们可以定义mn,“m小于或等于n”,意味着存在一个k使得m+k=n。如果我们这样做,就不难证明,对于每个nm, 和k

We can define mn, “m is less than or equal to n,” to mean that there exists a k such that m+k=n. If we do that, it is not hard to show that the less-than-or-equal-to relation satisfies all the following properties, for every n, m, and k:

  • nn反身性

  • nn (reflexivity)

  • 如果nmmk然后nk及物性

  • if nm and mk then nk (transitivity)

  • 如果nmmn然后n=m反对称性

  • if nm and mn then n=m (antisymmetry)

  • 面向所有人nm, 任何一个nm或者mn是真的(全部

  • for all n and m, either nm or mn is true (totality)

  • 如果nm然后n+km+k

  • if nm then n+km+k

  • 如果n+km+k然后nm

  • if n+km+k then nm

  • 如果nm然后nkmk

  • if nm then nkmk

  • 如果mn然后m=n或者mn+1

  • if mn then m=n or mn+1

  • 0n

  • 0n

记得第 13 章中的前四项断言是线性顺序。注意,当我们写mn,我们的意思是nm

Remember from Chapter 13 that the first four items assert that is a linear order. Note that when we write mn, we mean nm.

那么,像往常一样,我们可以定义m<n意思是mnmn。在这种情况下,我们有mn当且仅当m<n或者m=n

As usual, then, we can define m<n to mean that mn and mn. In that case, we have that mn holds if and only if m<n or m=n.


命题。对于每一个mm+10

Proposition. For every m, m+10.

证明。否则,我们就会(m+1)+k=(m+k)+1=0对于一些k

Proof. Otherwise, we would have (m+1)+k=(m+k)+1=0 for some k.


尤其是m=0,我们有10

In particular, taking m=0, we have 10.


命题。我们有m<n当且仅当m+1n

Proposition. We have m<n if and only if m+1n.

证明。假设m<n。 然后mnmn。因此有一个k使得m+k=n,并且mn,我们有k0。 然后k=u+1对于一些u,这意味着我们有m+(u+1)=m+1+u=n, 所以mn,按要求。

Proof. Suppose m<n. Then mn and mn. So there is a k such that m+k=n, and since mn, we have k0. Then k=u+1 for some u, which means we have m+(u+1)=m+1+u=n, so mn, as required.

在另一个方向上,假设m+1n。 然后mn.我们还有mn,因为如果m=n,我们会m+1m+0因此10,矛盾。

In the other direction, suppose m+1n. Then mn. We also have mn, since if m=n, we would have m+1m+0 and hence 10, a contradiction.


以类似的方式,我们可以证明m<n当且仅当mnmn。事实上,我们可以从这些属性和的属性中证明以下所有内容

In a similar way, we can show that m<n if and only if mn and mn. In fact, we can demonstrate all of the following from these properties and the properties of :

  • n<n永远不会是真的(非反身性

  • n<n is never true (irreflexivity)

  • 如果n<mm<k然后n<k及物性

  • if n<m and m<k then n<k (transitivity)

  • 面向所有人nm, 任何一个n<mn=m或者m<n为真(三分法

  • for all n and m, either n<m, n=m or m<n is true (trichotomy)

  • 如果n<m然后n+k<m+k

  • if n<m then n+k<m+k

  • 如果k>0n<m然后nk<mk

  • if k>0 and n<m then nk<mk

  • 如果m>n然后m=n+1或者m>n+1

  • if m>n then m=n+1 or m>n+1

  • 面向所有人nn=0或者n>0

  • for all n, n=0 or n>0

前三项意味着<是严格的线性顺序,并且上述属性意味着是相关的线性顺序,其含义在第 13.1 节中描述。

The first three items mean that < is a strict linear order, and the properties above means that is the associated linear order, in the sense described in Section 13.1.


证明。我们将利用先前对小于关系的描述来证明其中一些属性。

Proof. We will prove some of these properties using the previous characterization of the less-than relation.

第一个性质很简单:我们知道nn+1,如果我们有n+1n,我们应该n=n+1,矛盾。

The first property is straightforward: we know nn+1, and if we had n+1n, we should have n=n+1, a contradiction.

对于第二个属性,假设n<mm<k。 然后n+1mm+1k,这意味着n<k

For the second property, assume n<m and m<k. Then n+1mm+1k, which implies n<k.

对于第三种情况,我们知道nm或者mn。 如果m=n,我们就完成了,否则我们要么n<m或者m<n

For the third, we know that either nm or mn. If m=n, we are done, and otherwise we have either n<m or m<n.

对于第四个,如果n+1m,我们有n+1+k=(n+k)+1m+k,按要求。

For the fourth, if n+1m, we have n+1+k=(n+k)+1m+k, as required.

对于第五个,假设k>0,也就是说,k1。 如果n<m, 然后n+1m, 所以nk+1nk+kmk.但这意味着nk<mk,按要求。

For the fifth, suppose k>0, which is to say, k1. If n<m, then n+1m, and so nk+1nk+kmk. But this implies nk<mk, as required.

其余的证明留给读者作为练习。

The rest of the remaining proofs are left as an exercise to the reader.


以下是一些附加属性<

Here are some additional properties of < and :

  • n<mm<n不能同时成立(不对称

  • n<m and m<n cannot both hold (asymmetry)

  • n+1>n

  • n+1>n

  • 如果n<mmk然后n<k

  • if n<m and mk then n<k

  • 如果nmm<k然后n<k

  • if nm and m<k then n<k

  • 如果m>n然后mn+1

  • if m>n then mn+1

  • 如果mn然后m+1>n

  • if mn then m+1>n

  • 如果n+k<m+k然后n<m

  • if n+k<m+k then n<m

  • 如果nk<mk然后k>0n<m

  • if nk<mk then k>0 and n<m

这些可以从上面的证明中得到。此外,我们刚刚看到的原理集合可用于证明自然数的基本事实,这些事实在非正式数学论证中通常被视为理所当然。

These can be proved from the ones above. Moreover, the collection of principles we have just seen can be used to justify basic facts about the natural numbers, which are again typically taken for granted in informal mathematical arguments.


命题。如果mn是自然数,例如m+n=0, 然后m=n=0

Proposition. If m and n are natural numbers such that m+n=0, then m=n=0.

证明。如果m+n=0, 然后m0, 所以m=0n=0+n=m+n=0

Proof. If m+n=0, then m0, so m=0 and n=0+n=m+n=0.

命题。如果n是一个自然数,n<3, 然后n=0n=1或者n=2

Proposition. If n is a natural number such that n<3, then n=0, n=1 or n=2.

证明。在这个证明中,我们反复使用以下属性:m>n然后m=n+1或者m>n+1。 自从2+1=3>n,我们得出的结论是2+1=n+1或者2+1>n+1在第一种情况下,我们得出结论n=2,我们就完成了。在第二种情况下,我们得出结论2>n,这意味着2=n+1, 或者2>n+1在第一种情况下,我们得出结论n=1,我们就完成了。在第二种情况下,我们得出结论1>n,并最后一次援引上面提出的一般原则来得出结论:1=n+1或者1>n+1在第一种情况下,我们得出结论n=0,我们又一次完成了。在第二种情况下,我们得出的结论是0>n。这导致了一个矛盾,因为现在0>n0, 因此0>0,这与>

Proof. In this proof we repeatedly use the property that if m>n then m=n+1 or m>n+1. Since 2+1=3>n, we conclude that either 2+1=n+1 or 2+1>n+1. In the first case we conclude n=2, and we are done. In the second case we conclude 2>n, which implies that either 2=n+1, or 2>n+1. In the first case, we conclude n=1, and we are done. In the second case, we conclude 1>n, and appeal one last time to the general principle presented above to conclude that either 1=n+1 or 1>n+1. In the first case, we conclude n=0, and we are once again done. In the second case, we conclude that 0>n. This leads to a contradiction, since now 0>n0, hence 0>0, which contradicts the irreflexivity of >.


17.6.整数

17.6. The Integers

自然数是为计数离散量而设计的,但它们有一个令人讨厌的缺点:可以减去nm如果n小于或等于m,但如果m大于n.整数集,Z,用负值扩展自然数,以便可以完全进行减法:

The natural numbers are designed for counting discrete quantities, but they suffer an annoying drawback: it is possible to subtract n from m if n is less than or equal to m, but not if m is greater than n. The set of integers, Z, extends the natural numbers with negative values, to make it possible to carry out subtraction in full:

Z={,3,2,1,0,1,2,3,}.

在后面的章节中,我们将看到整数可以扩展为有理数复数,它们各有其用途。然而,对于处理离散量,整数将大有裨益。

We will see in a later chapter that the integers can be extended to the rational numbers, the real numbers, and the complex numbers, each of which serves useful purposes. For dealing with discrete quantities, however, the integers will get us pretty far.

你可以将整数视为由两个自然数组成,一个正数和一个负数,它们共享一个零。相反,一旦我们有了整数,你就可以把自然数视为由非负整数组成,即大于或等于0大多数数学家模糊了两者之间的区别,尽管我们会看到,例如在 Lean 中,自然数和整数代表两种不同的数据类型。

You can think of the integers as consisting of two copies of the natural numbers, a positive one and a negative one, sharing a common zero. Conversely, once we have the integers, you can think of the natural numbers as consisting of the nonnegative integers, that is, the integers that are greater than or equal to 0. Most mathematicians blur the distinction between the two, though we will see that in Lean, for example, the natural numbers and the integers represent two different data types.

上一节列举的自然数的大多数性质也适用于整数,但不是全部。例如,不再有n+10对于每一个n,因为这个说法是错误的n=1再举一个例子,并不是每个整数都等于0或大于0,因为这不适用于负整数。

Most of the properties of the natural numbers that were enumerated in the last section hold of the integers as well, but not all. For example, it is no longer the case that n+10 for every n, since the claim is false for n=1. For another example, it is not the case that every integer is either equal to 0 or greater than 0, since this fails to hold of the negative integers.

整数与自然数的一个关键区别是,对于每个整数n有一个价值n具有以下属性n+(n)=0. 价值n被称为否定n.我们定义减法nmn+(m). 对于任何整数n,我们还定义了绝对值n,书面|n|,即n如果n0, 和n否则。

The key property that the integers enjoy, which sets them apart from the natural numbers, is that for every integer n there is a value n with the property that n+(n)=0. The value n is called the negation of n. We define subtraction nm to be n+(m). For any integer n, we also define the absolute value of n, written |n|, to be n if n0, and n otherwise.

我们不再能用归纳法证明整数,因为归纳法不涵盖负数。但是,例如,我们可以使用归纳法来证明某个属性适用于每个非负整数。此外,我们知道每个负整数都是正整数的负数。因此,涉及整数的证明通常分为两种情况,一种情况涵盖非负整数,另一种情况涵盖负整数。

We can no longer use proof by induction on the integers, because induction does not cover the negative numbers. But we can use induction to show that a property holds of every nonnegative integer, for example. Moreover, we know that every negative integer is the negation of a positive one. As a result, proofs involving the integers often break down into two cases, where one case covers the nonnegative integers, and the other case covers the negative ones.

17.7.练习

17.7. Exercises

  1. 用符号逻辑的符号写出完全归纳原理。同样用这种方式写出最小元素原理,并使用逻辑运算来证明两者是等价的。

  2. Write the principle of complete induction using the notation of symbolic logic. Also write the least element principle this way, and use logical manipulations to show that the two are equivalent.

  3. 证明对于每一个n02+12+22+n2=16n(1+n)(1+2n)

  4. Show that for every n, 02+12+22+n2=16n(1+n)(1+2n).

  5. 证明对于每一个n03+13++n3=14n2(n+1)2

  6. Show that for every n, 03+13++n3=14n2(n+1)2.

  7. 证明对于每一个nini(i+1)!=n!1n

  8. Show that for every n, ini(i+1)!=n!1n.

  9. 给出第 17.3 节中斐波那契数的定义,证明卡西尼恒等式:对于每个nFn+12Fn+2Fn=(1)n提示:在归纳步骤中,写下Fn+22作为Fn+2(Fn+1+Fn)

  10. Given the definition of the Fibonacci numbers in Section 17.3, prove Cassini’s identity: for every n, Fn+12Fn+2Fn=(1)n. Hint: in the induction step, write Fn+22 as Fn+2(Fn+1+Fn).

  11. 证明i<nF2i+1=F2n

  12. Prove i<nF2i+1=F2n.

  13. 证明以下两个恒等式:

    • F2n+1=Fn+12+Fn2

    • F2n+2=Fn+22Fn2

    提示:使用归纳法n,并同时证明它们。在归纳步骤中,展开F2n+3=F2n+2+F2n+1,同样地F2n+4证明第二个方程尤其棘手。使用归纳假设和第一个恒等式来简化左边,并反复展开指数最高的斐波那契数,简化需要证明的方程。(当你算出解法时,写出一个清晰的方程证明,按“正向”方向计算。)

  14. Prove the following two identities:

    • F2n+1=Fn+12+Fn2

    • F2n+2=Fn+22Fn2

    Hint: use induction on n, and prove them both at once. In the induction step, expand F2n+3=F2n+2+F2n+1, and similarly for F2n+4. Proving the second equation is especially tricky. Use the inductive hypothesis and the first identity to simplify the left-hand side, and repeatedly unfold the Fibonacci number with the highest index and simplify the equation you need to prove. (When you have worked out a solution, write a clear equational proof, calculating in the ``forward’’ direction.)

  15. 证明每个自然数都可以写成2 的不同幂的和。对于这个问题,1=20被算作2的幂。

  16. Prove that every natural number can be written as a sum of distinct powers of 2. For this problem, 1=20 is counted as power of 2.

  17. V是一组非空整数,满足以下两个属性:

    • 如果x,yV, 然后xyV

    • 如果xV,然后每个倍数x是...的一个元素V

    证明存在dV,这样V等于d.提示:利用最小元素原理。

  18. Let V be a non-empty set of integers such that the following two properties hold:

    • If x,yV, then xyV.

    • If xV, then every multiple of x is an element of V.

    Prove that there is some dV, such that V is equal to the set of multiples of d. Hint: use the least element principle.

  19. 给出一个非正式但详细的证明,对于每个自然数n1n=n,使用归纳证明、乘法定义和第 17.4 节中证明的定理。

  20. Give an informal but detailed proof that for every natural number n, 1n=n, using a proof by induction, the definition of multiplication, and the theorems proved in Section 17.4.

  21. 证明乘法对加法有分配作用。换句话说,证明对于自然数mn, 和km(n+k)=mn+mk。您应该使用加法和乘法的定义以及第 17.4 节中证明的事实(但仅此而已)。

  22. Show that multiplication distributes over addition. In other words, prove that for natural numbers m, n, and k, m(n+k)=mn+mk. You should use the definitions of addition and multiplication and facts proved in Section 17.4 (but nothing more).

  23. 用同样的方法证明乘法符合结合律。你可以使用第 17.4 节和上一个练习中证明的任何事实。

  24. Prove the multiplication is associative, in the same way. You can use any of the facts proved in Section 17.4 and the previous exercise.

  25. 证明乘法是可交换的。

  26. Prove that multiplication is commutative.

  27. 证明(mn)k=mnk

  28. Prove (mn)k=mnk.

  29. 按照第 17.5 节中的例子,证明如果n是一个自然数,n<5, 然后n是价值观之一0,1,2,3, 或者4

  30. Following the example in Section 17.5, prove that if n is a natural number and n<5, then n is one of the values 0,1,2,3, or 4.

  31. 证明如果nm是自然数,nm=1, 然后n=m=1,仅使用第 17.5 节中列出的属性。

    这很棘手。首先说明nm大于0,因此大于或等于1。然后证明如果其中任何一个大于1, 然后nm>1

  32. Prove that if n and m are natural numbers and nm=1, then n=m=1, using only properties listed in Section 17.5.

    This is tricky. First show that n and m are greater than 0, and hence greater than or equal to 1. Then show that if either one of them is greater than 1, then nm>1.

  33. 证明第 17.5 节中任何其他未经证明的主张。

  34. Prove any of the other claims in Section 17.5 that were stated without proof.

  35. 仅使用第 17.6 节中给出的否定和减法性质,证明以下整数否定和减法性质。

    • 如果n+m=0然后m=n

    • 0=0

    • 如果n=m然后n=m

    • m+(nm)=n

    • (n+m)=nm

    • 如果m<n然后nm>0

    • 如果m<n然后m>n

    • n(m)=nm

    • n(mk)=nmnk

    • 如果n<m然后nk<mk

  36. Prove the following properties of negation and subtraction on the integers, using only the properties of negation and subtraction given in Section 17.6.

    • If n+m=0 then m=n.

    • 0=0.

    • If n=m then n=m.

    • m+(nm)=n.

    • (n+m)=nm.

    • If m<n then nm>0.

    • If m<n then m>n.

    • n(m)=nm.

    • n(mk)=nmnk.

    • If n<m then nk<mk.

  37. 假设你有一个无限大的棋盘,每个方格上都写有一个自然数。每个方格上的数值是四个相邻方格数值的平均值。请证明棋盘上的所有数值都相等。

  38. Suppose you have an infinite chessboard with a natural number written in each square. The value in each square is the average of the values of the four neighboring squares. Prove that all the values on the chessboard are equal.

  39. 证明每个自然数都可以写成不同的非连续斐波那契数的和。例如,22=1+3+5+13是不允许的,因为 3 和 5 是连续的斐波那契数,但是22=1+21是允许的。

  40. Prove that every natural number can be written as a sum of distinct non-consecutive Fibonacci numbers. For example, 22=1+3+5+13 is not allowed, since 3 and 5 are consecutive Fibonacci numbers, but 22=1+21 is allowed.

18.精益中的自然数与归纳法

18. The Natural Numbers and Induction in Lean

18.1. Lean 中的归纳和递归

18.1. Induction and Recursion in Lean

zero在 Lean 内部,自然数被定义为通过公理声明的and运算归纳生成的类型succ

Internally, in Lean, the natural numbers are defined as a type generated inductively from an axiomatically declared zero and succ operation:

inductive nat : Type
| zero : nat
| succ : nat  nat
inductive nat : Type
| zero : nat
| succ : nat  nat

如果您点击按钮将此文本复制到本教科书在线版的编辑器中,您将看到我们用短语和将其包裹起来。这会将定义放入新的“命名空间”,以便定义的标识符为、和,以避免与 Lean 库中的标识符冲突。下面,我们将在示例重复库中定义的对象的多个地方执行此操作。用或输入的 unicode 符号是 的同义词。namespace hiddenend hiddenhidden.nathidden.nat.zerohidden.nat.succ\N\natnat

If you click the button that copies this text into the editor in the online version of this textbook, you will see that we wrap it with the phrases namespace hidden and end hidden. This puts the definition into a new “namespace,” so that the identifiers that are defined are hidden.nat, hidden.nat.zero and hidden.nat.succ, to avoid conflicting with the one that is in the Lean library. Below, we will do that in a number of places where our examples duplicate objects defined in the library. The unicode symbol , entered with \N or \nat, is a synonym for nat.

声明nat为归纳定义类型意味着我们可以通过递归定义函数,并通过归纳证明定理。例如,这是上一章中介绍的前两个递归定义:

Declaring nat as an inductively defined type means that we can define functions by recursion, and prove theorems by induction. For example, these are the first two recursive definitions presented in the last chapter:

open nat

def two_pow :   
| 0        := 1
| (succ n) := 2 * two_pow n

def fact :   
| 0        := 1
| (succ n) := (succ n) * fact n
open nat

def two_pow :   
| 0        := 1
| (succ n) := 2 * two_pow n

def fact :   
| 0        := 1
| (succ n) := (succ n) * fact n

加法和数字的定义方式使得 Lean 认为它们本质上是相同的,因此我们可以将这些定义写如下:succ nn + 1

Addition and numerals are defined in such a way that Lean recognizes succ n and n + 1 as essentially the same, so we could instead write these definitions as follows:

def two_pow :   
| 0       := 1
| (n + 1) := 2 * two_pow n

def fact :   
| 0       := 1
| (n + 1) := (n + 1) * fact n
def two_pow :   
| 0       := 1
| (n + 1) := 2 * two_pow n

def fact :   
| 0       := 1
| (n + 1) := (n + 1) * fact n

如果我们想要定义函数m^n,我们可以通过修复m,并对第二个参数执行递归来实现:

If we wanted to define the function m^n, we would do that by fixing m, and writing doing the recursion on the second argument:

def pow :     
| m 0        := 1
| m (n + 1)  := m * pow m n
def pow :     
| m 0        := 1
| m (n + 1)  := m * pow m n

nat.pow事实上,这就是Lean 库中自然数幂函数的定义。

In fact, this is how the power function on the natural numbers, nat.pow, is defined in Lean’s library.

Lean 也足够智能,可以解释更复杂的递归形式,例如这个:

Lean is also smart enough to interpret more complicated forms of recursion, like this one:

def fib :   
| 0        := 0
| 1        := 1
| (n + 2)  := fib (n + 1) + fib n
def fib :   
| 0        := 0
| 1        := 1
| (n + 2)  := fib (n + 1) + fib n

除了用递归定义函数外,我们还可以用归纳法证明定理。在精益中,递归定义的每个子句都会产生一个新的恒等式。例如,上述定义中的两个子句产生了pow以下两个定理:

In addition to defining functions by recursion, we can prove theorems by induction. In Lean, each clause of a recursive definition results in a new identity. For example, the two clauses in the definition of pow above give rise to the following two theorems:

import data.nat.pow
open nat

example (n : ) : pow n 0 = 1 := rfl
example (m n : ) : pow m (n+1) = m * pow m n := rfl
import data.nat.pow
open nat

example (n : ) : pow n 0 = 1 := rfl
example (m n : ) : pow m (n+1) = m * pow m n := rfl

Lean 定义了指数的常用符号:

Lean defines the usual notation for exponentiation:

import data.nat.pow
open nat

example (n : ) : n^0 = 1 := rfl
example (m n : ) : m^(n+1) = m * m^n := rfl

#check @pow_zero
#check @pow_succ
import data.nat.pow
open nat

example (n : ) : n^0 = 1 := rfl
example (m n : ) : m^(n+1) = m * m^n := rfl

#check @pow_zero
#check @pow_succ

请注意,我们也可以在 定义的第二条子句中使用。当然,我们可以使用乘法的交换性来证明这两个定义是等价的,但是,使用归纳证明,我们也可以仅使用乘法的结合性以及 和 的性质来证明它。这很有用,因为幂函数也经常用于乘法不交换的情况,例如矩阵乘法。该定理可以在 Lean 中证明如下:pow m n * npow1 * m = mm * 1 = m

Notice that we could alternatively have used pow m n * n in the second clause of the definition of pow. Of course, we can prove that the two definitions are equivalent using the commutativity of multiplication, but, using a proof by induction, we can also prove it using only the associativity of multiplication, and the properties 1 * m = m and m * 1 = m. This is useful, because the power function is also often used in situations where multiplication is not commutative, such as with matrix multiplication. The theorem can be proved in Lean as follows:

import data.nat.pow
open nat

example (m n : ) : m^(succ n) = m^n * m :=
nat.rec_on n
  (show m^(succ 0) = m^0 * m, from calc
    m^(succ 0) = m * m^0 : by rw pow_succ
          ... = m * 1   : by rw pow_zero
          ... = m       : by rw mul_one
          ... = 1 * m   : by rw one_mul
          ... = m^0 * m : by rw pow_zero)
  (assume n,
    assume ih : m^(succ n) = m^n * m,
    show m^(succ (succ n)) = m^(succ n) * m, from calc
      m^(succ (succ n)) = m * m^(succ n)   : by rw pow_succ
                    ... = m * (m^n * m)    : by rw ih
                    ... = (m * m^n) * m    : by rw mul_assoc
                    ... = m^(succ n) *m    : by rw pow_succ)
import data.nat.pow
open nat

example (m n : ) : m^(succ n) = m^n * m :=
nat.rec_on n
  (show m^(succ 0) = m^0 * m, from calc
    m^(succ 0) = m * m^0 : by rw pow_succ
          ... = m * 1   : by rw pow_zero
          ... = m       : by rw mul_one
          ... = 1 * m   : by rw one_mul
          ... = m^0 * m : by rw pow_zero)
  (assume n,
    assume ih : m^(succ n) = m^n * m,
    show m^(succ (succ n)) = m^(succ n) * m, from calc
      m^(succ (succ n)) = m * m^(succ n)   : by rw pow_succ
                    ... = m * (m^n * m)    : by rw ih
                    ... = (m * m^n) * m    : by rw mul_assoc
                    ... = m^(succ n) *m    : by rw pow_succ)

这是精益中典型的归纳证明。它以短语 开始,然后是基本情况和归纳假设。(该短语允许我们写而不是。这个定理在库中被调用。证明可以使用 来缩短:nat.rec_on nopen natpownat.powpow_succ'rewrite

This is a typical proof by induction in Lean. It begins with the phrase nat.rec_on n, and is followed by the base case and the inductive hypothesis. (The phrase open nat allows us to write pow instead of nat.pow. This theorem is called pow_succ' in the library.The proof can be shortened using rewrite:

import data.nat.pow
open nat

example (m n : ) : m^(succ n) = m^n * m :=
nat.rec_on n
  (show m^(succ 0) = m^0 * m, by
    rw [pow_succ, pow_zero, mul_one, one_mul])
  (assume n,
    assume ih : m^(succ n) = m^n * m,
    show m^(succ (succ n)) = m^(succ n) * m,
      by rw [pow_succ, pow_succ, mul_assoc, ih, pow_succ])
import data.nat.pow
open nat

example (m n : ) : m^(succ n) = m^n * m :=
nat.rec_on n
  (show m^(succ 0) = m^0 * m, by
    rw [pow_succ, pow_zero, mul_one, one_mul])
  (assume n,
    assume ih : m^(succ n) = m^n * m,
    show m^(succ (succ n)) = m^(succ n) * m,
      by rw [pow_succ, pow_succ, mul_assoc, ih, pow_succ])

请记住,您可以逐步编写rewrite证明,检查错误消息以确保到目前为止一切正常,并查看 Lean 的进展情况。

Remember that you can write a rewrite proof incrementally, checking the error messages to make sure things are working so far, and to see how far Lean got.

作为归纳证明的另一个例子,这里是恒等式的证明。m^(n + k) = m^n * m^k

As another example of a proof by induction, here is a proof of the identity m^(n + k) = m^n * m^k.

import data.nat.pow
open nat

example (m n k : ) : m^(n + k) = m^n * m^k :=
nat.rec_on k
  (show m^(n + 0) = m^n * m^0, from calc
    m^(n + 0) = m^n       : by rw add_zero
          ... = m^n * 1   : by rw mul_one
          ... = m^n * m^0 : by rw pow_zero)
  (assume k,
    assume ih : m^(n + k) = m^n * m^k,
    show m^(n + succ k) = m^n * m^(succ k), from calc
      m^(n + succ k) = m^(succ (n + k)) : by rw nat.add_succ
                ... = m^(n + k) * m    : by rw pow_succ'
                ... = m^n * m^k * m    : by rw ih
                ... = m^n * (m^k * m)  : by rw mul_assoc
                ... = m^n * m^(succ k) : by rw pow_succ')
import data.nat.pow
open nat

example (m n k : ) : m^(n + k) = m^n * m^k :=
nat.rec_on k
  (show m^(n + 0) = m^n * m^0, from calc
    m^(n + 0) = m^n       : by rw add_zero
          ... = m^n * 1   : by rw mul_one
          ... = m^n * m^0 : by rw pow_zero)
  (assume k,
    assume ih : m^(n + k) = m^n * m^k,
    show m^(n + succ k) = m^n * m^(succ k), from calc
      m^(n + succ k) = m^(succ (n + k)) : by rw nat.add_succ
                ... = m^(n + k) * m    : by rw pow_succ'
                ... = m^n * m^k * m    : by rw ih
                ... = m^n * (m^k * m)  : by rw mul_assoc
                ... = m^n * m^(succ k) : by rw pow_succ')

注意相同的模式。这一次,我们对 进行归纳k,并且起始情况和归纳步骤都是常规的。该定理pow_add在库中被调用,再一次,凭借一点小聪明,我们可以用 来缩短证明rewrite

Notice the same pattern. This time, we do induction on k, and the base case and inductive step are routine. The theorem is called pow_add in the library, and once again, with a bit of cleverness, we can shorten the proof with rewrite:

import data.nat.pow
open nat

example (m n k : ) : m^(n + k) = m^n * m^k :=
nat.rec_on k
  (show m^(n + 0) = m^n * m^0,
    by rw [add_zero, pow_zero, mul_one])
  (assume k,
    assume ih : m^(n + k) = m^n * m^k,
    show m^(n + succ k) = m^n * m^(succ k),
    by rw [nat.add_succ, pow_succ', ih, mul_assoc, pow_succ'])
import data.nat.pow
open nat

example (m n k : ) : m^(n + k) = m^n * m^k :=
nat.rec_on k
  (show m^(n + 0) = m^n * m^0,
    by rw [add_zero, pow_zero, mul_one])
  (assume k,
    assume ih : m^(n + k) = m^n * m^k,
    show m^(n + succ k) = m^n * m^(succ k),
    by rw [nat.add_succ, pow_succ', ih, mul_assoc, pow_succ'])

但是,您应该毫不犹豫地使用calc,以使证明更加明确。请记住,您也可以一起使用calc和,使用来构造计算证明,使用来填写每个证明步骤。rewritecalcrewrite

You should not hesitate to use calc, however, to make the proofs more explicit. Remember that you can also use calc and rewrite together, using calc to structure the calculational proof, and using rewrite to fill in each justification step.

18.2.定义 Lean 中的算术运算

18.2. Defining the Arithmetic Operations in Lean

事实上,加法和乘法在 Lean 中的定义基本与第 17.4 节中所述一致。加法的定义方程符合反身性,但它们也被称为add_zeroadd_succ

In fact, addition and multiplication are defined in Lean essentially as described in Section 17.4. The defining equations for addition hold by reflexivity, but they are also named add_zero and add_succ:

import data.nat.basic
open nat

variables m n : 

example : m + 0 = m := add_zero m
example : m + succ n = succ (m + n) := add_succ m n
import data.nat.basic
open nat

variables m n : 

example : m + 0 = m := add_zero m
example : m + succ n = succ (m + n) := add_succ m n

类似地,我们有前导函数和乘法的定义方程:

Similarly, we have the defining equations for the predecessor function and multiplication:

import data.nat.basic
open nat

#check @pred_zero
#check @pred_succ
#check @mul_zero
#check @mul_succ
import data.nat.basic
open nat

#check @pred_zero
#check @pred_succ
#check @mul_zero
#check @mul_succ

以下是第17.4节中证明的五个命题。

Here are the five propositions proved in Section 17.4.

theorem succ_pred (n : ) : n  0  succ (pred n) = n :=
nat.rec_on n
  (assume H : 0  0,
    show succ (pred 0) = 0, from absurd rfl H)
  (assume n,
    assume ih,
    assume H : succ n  0,
    show succ (pred (succ n)) = succ n,
      by rewrite pred_succ)

theorem zero_add (n : nat) : 0 + n = n :=
nat.rec_on n
  (show 0 + 0 = 0, from rfl)
  (assume n,
    assume ih : 0 + n = n,
    show 0 + succ n = succ n, from
      calc
    0 + succ n = succ (0 + n) : rfl
      ... = succ n : by rw ih)

theorem succ_add (m n : nat) : succ m + n = succ (m + n) :=
nat.rec_on n
  (show succ m + 0 = succ (m + 0), from rfl)
  (assume n,
    assume ih : succ m + n = succ (m + n),
    show succ m + succ n = succ (m + succ n), from
      calc
    succ m + succ n = succ (succ m + n) : rfl
      ... = succ (succ (m + n)) : by rw ih
      ... = succ (m + succ n) : rfl)

theorem add_assoc (m n k : nat) : m + n + k = m + (n + k) :=
nat.rec_on k
  (show m + n + 0 = m + (n + 0), by rw [add_zero, add_zero])
  (assume k,
    assume ih : m + n + k = m + (n + k),
    show m + n + succ k = m + (n + (succ k)), from calc
      m + n + succ k = succ (m + n + k)   : by rw add_succ
                 ... = succ (m + (n + k)) : by rw ih
                 ... = m + succ (n + k)   : by rw add_succ
                 ... = m + (n + succ k)   : by rw add_succ)

theorem add_comm (m n : nat) : m + n = n + m :=
nat.rec_on n
  (show m + 0 = 0 + m, by rewrite [add_zero, zero_add])
  (assume n,
    assume ih : m + n = n + m,
    show m + succ n = succ n + m, from calc
      m + succ n = succ (m + n) : by rw add_succ
             ... = succ (n + m) : by rw ih
             ... = succ n + m   : by rw succ_add)
theorem succ_pred (n : ) : n  0  succ (pred n) = n :=
nat.rec_on n
  (assume H : 0  0,
    show succ (pred 0) = 0, from absurd rfl H)
  (assume n,
    assume ih,
    assume H : succ n  0,
    show succ (pred (succ n)) = succ n,
      by rewrite pred_succ)

theorem zero_add (n : nat) : 0 + n = n :=
nat.rec_on n
  (show 0 + 0 = 0, from rfl)
  (assume n,
    assume ih : 0 + n = n,
    show 0 + succ n = succ n, from
      calc
    0 + succ n = succ (0 + n) : rfl
      ... = succ n : by rw ih)

theorem succ_add (m n : nat) : succ m + n = succ (m + n) :=
nat.rec_on n
  (show succ m + 0 = succ (m + 0), from rfl)
  (assume n,
    assume ih : succ m + n = succ (m + n),
    show succ m + succ n = succ (m + succ n), from
      calc
    succ m + succ n = succ (succ m + n) : rfl
      ... = succ (succ (m + n)) : by rw ih
      ... = succ (m + succ n) : rfl)

theorem add_assoc (m n k : nat) : m + n + k = m + (n + k) :=
nat.rec_on k
  (show m + n + 0 = m + (n + 0), by rw [add_zero, add_zero])
  (assume k,
    assume ih : m + n + k = m + (n + k),
    show m + n + succ k = m + (n + (succ k)), from calc
      m + n + succ k = succ (m + n + k)   : by rw add_succ
                 ... = succ (m + (n + k)) : by rw ih
                 ... = m + succ (n + k)   : by rw add_succ
                 ... = m + (n + succ k)   : by rw add_succ)

theorem add_comm (m n : nat) : m + n = n + m :=
nat.rec_on n
  (show m + 0 = 0 + m, by rewrite [add_zero, zero_add])
  (assume n,
    assume ih : m + n = n + m,
    show m + succ n = succ n + m, from calc
      m + succ n = succ (m + n) : by rw add_succ
             ... = succ (n + m) : by rw ih
             ... = succ n + m   : by rw succ_add)

18.3.练习

18.3. Exercises

  1. 通过用证明替换每个遗憾,尽可能多地形式化第 17.4 节中的身份。

    import data.nat.basic
    open nat
    
    --1.a.
    example :  m n k : nat, m * (n + k) = m * n + m * k := sorry
    
    --1.b.
    example :  n : nat, 0 * n = 0 := sorry
    
    --1.c.
    example :  n : nat, 1 * n = n := sorry
    
    --1.d.
    example :  m n k : nat, (m * n) * k = m * (n * k) := sorry
    
    --1.e.
    example :  m n : nat, m * n= n * m := sorry
    
  2. Formalize as many of the identities from Section 17.4 as you can by replacing each sorry with a proof.

    import data.nat.basic
    open nat
    
    --1.a.
    example :  m n k : nat, m * (n + k) = m * n + m * k := sorry
    
    --1.b.
    example :  n : nat, 0 * n = 0 := sorry
    
    --1.c.
    example :  n : nat, 1 * n = n := sorry
    
    --1.d.
    example :  m n k : nat, (m * n) * k = m * (n * k) := sorry
    
    --1.e.
    example :  m n : nat, m * n= n * m := sorry
    
  3. 通过用证明替换每个遗憾,尽可能多地形式化第 17.5 节中的身份。

    import data.nat.basic
    open nat
    
    --2.a.
    example :  m n k : nat, n  m  n + k  m  + k := sorry
    
    --2.b.
    example :  m n k : nat, n + k  m + k  n  m := sorry
    
    --2.c.
    example :  m n k : nat, n  m  n * k  m * k := sorry
    
    --2.d.
    example :  m n : nat, m  n  m = n  m  n+1 := sorry
    
    --2.e.
    example :  n : nat, 0  n := sorry
    
  4. Formalize as many of the identities from Section 17.5 as you can by replacing each sorry with a proof.

    import data.nat.basic
    open nat
    
    --2.a.
    example :  m n k : nat, n  m  n + k  m  + k := sorry
    
    --2.b.
    example :  m n k : nat, n + k  m + k  n  m := sorry
    
    --2.c.
    example :  m n k : nat, n  m  n * k  m * k := sorry
    
    --2.d.
    example :  m n : nat, m  n  m = n  m  n+1 := sorry
    
    --2.e.
    example :  n : nat, 0  n := sorry
    

19.初等数论

19. Elementary Number Theory

在前两章中,我们看到自然数的特点是它们支持归纳证明递归定义。此外,有了这些组成部分,我们实际上可以定义+×, 和<在适当的公理基础上,证明了它们具有相应的性质。在17.6节中,我们还讨论了整数,包括负数,并且支持减法运算。

In the last two chapters, we saw that the natural numbers are characterized by the fact that they support proof by induction and definition by recursion. Moreover, with these components, we can actually define +, ×, and < in a suitable axiomatic foundation, and prove that they have the relevant properties. In Section 17.6 we also discussed the integers, which include negative numbers and support the operation of subtraction.

自然数和整数是数论的核心组成部分,数论是数学的一个分支,可以追溯到古代。在本章中,我们将讨论该主题的一些基本知识。

The natural numbers and the integers are the central components of number theory, a branch of mathematics dating back to the ancients. In this chapter, we will discuss some of the rudiments of this subject.

19.1.商余数定理

19.1. The Quotient-Remainder Theorem

我们将在这里使用的整数的一个关键属性是商余数定理:

A key property of the integers that we will use here is the quotient-remainder theorem:


定理。nm为整数m>0. 然后有整数qr令人满意n=mq+r0r<m

Theorem. Let n and m be integers with m>0. Then there are integers q and r satisfying n=mq+r and 0r<m.

证明。首先,我们证明以下情况n是一个自然数,在这种情况下使用完全归纳法n。 让n是任意自然数。如果n<m,然后我们可以采取q=0r=n,我们确实有n=mq+r0r<m否则,我们有nm。 在这种情况下nm是小于的自然数n。根据归纳假设,我们知道可以找到qr使得nm=mq+r0r<m.然后我们可以选择q=q+1r=r,我们得到n=mq+r0r<m,根据需要。

Proof. First we prove this in the case where n is a natural number, in which case use complete induction on n. Let n be any natural number. If n<m, then we can take q=0 and r=n, and we indeed have n=mq+r and 0r<m. Otherwise, we have nm. In this case nm is a natural number smaller than n. By induction hypothesis, we know that we can find q and r such that nm=mq+r and 0r<m. Then we can choose q=q+1 and r=r, and we obtain n=mq+r and 0r<m, as desired.

如果n为负,则(n+1)是一个自然数,因此我们可以使用前面的部分来表示(n+1)获得qr使得(n+1)=mq+r0r<m.现在让q=(q+1)r=mr1.然后我们可以计算

If n is negative, then (n+1) is a natural number, hence we can use the previous part for (n+1) to obtain q and r such that (n+1)=mq+r and 0r<m. Now let q=(q+1) and r=mr1. Then we can compute

mq+r=m(q+1)+mr1=(mq+r)m+m1=((n+1))1=n+11=n.

此外,由于r0我们有r<m并且从那时起r<m我们有r0。这样就完成了证明。

Also, since r0 we have r<m and since r<m we have r0. This completes the proof.


直观地看,q是除法后的整数n经过mr余数。请记住,使用“the”一词意味着存在符合该描述的唯一值。事实上,情况确实如此:

Intuitively, q is the integer quotient when you divide n by m and r is the remainder. Remember that using the word “the” presupposes that there are unique values meeting that description. That is, in fact, the case:


命题。如果nm如上所述,n=mq+rn=mq+r两者兼而有之rr少于m, 然后q=qr=r

Proposition. If n and m are as above, n=mq+r and n=mq+r with both r and r less than m, then q=q and r=r.

证明。根据假设,我们有mq+r=mq+r足以表明q=q,因为那时mq=mq,因此r=r

Proof. By assumption, we have mq+r=mq+r. It suffices to show that q=q, because then mq=mq, and hence r=r.

认为qq. 然后q<q或者q<q. 不失一般性地假设q<q(另一种情况是对称的。)那么mq<mq,所以我们可以减去mq从平等的两边mq+r=mq+r获得

Suppose qq. Then either q<q or q<q. Suppose without loss of generality that q<q. (The other case is symmetric.) Then mq<mq, so we can subtract mq from both sides of the equality mq+r=mq+r to obtain

r=mq+rmq=m(qq)+r.

但自从q<q,我们有qq1,这意味着

But since q<q, we have qq1, which means

m(qq)+rm+rm,

这与以下事实相矛盾:r<m

which contradicts the fact that r<m.


19.2.可除性

19.2. Divisibility

我们可以按如下方式定义整数的可整除性。

We can define divisibility on the integers as follows.


定义。给定两个整数mn,我们说m 是除数 n,书面mn,如果存在某个整数k使得mk=n我们还说n 可以被整除 m或者m 分裂 n.我们写mnm不是除数n

Definition. Given two integers m and n, we say that m is a divisor of n, written mn, if there exists some integer k such that mk=n. We also say that n is divisible by m or that m divides n. We write mn to say that m is not a divisor of n.


我们现在可以证明以下内容:

We can now prove the following:


定理。关系是自反的和传递的。另外,如果nmmn, 然后m=±n。这意味着,限制于自然数,这种关系是偏序的。

Theorem. The relation is reflexive and transitive. Also, if nm and mn, then m=±n. This means that restricted to the natural numbers, this relation is a partial order.

证明。反身性是直接的,因为n1=n, 因此nn

Proof. Reflexivity is immediate, because n1=n, hence nn.

对于传递性,假设mnnr. 然后还有k,使得mk=nn=r.现在我们计算

For transitivity, suppose mn and nr. Then there are k, such that mk=n and n=r. Now we compute

m(k)=(mk)=n=r.

假设nm是整数,并且nmmn. 那么存在k使得nk=mm=n。我们区分两种情况。如果n=0,那么我们有m=nk=0=n,这样我们就完成了。如果n0,然后我们利用方程得到nk=m=n,我们可以取消n两侧k=1.我们得出的结论是k==±1,因此我们得到m=nk=±n

Suppose that n and m are integers such that nm and mn. Then there exist k and such that nk=m and m=n. We distinguish two cases. If n=0, then we have m=nk=0=n, so we are done. If n0, then we use the the equations to get nk=m=n, and we can cancel n on both sides to get k=1. We conclude that k==±1, hence we get m=nk=±n.

请注意,这意味着如果nm都是自然数,那么n=m,这意味着在自然数上是反对称的,因此是偏序的。

Note that this means that if n and m are both natural numbers, then n=m, which means that is antisymmetric, and hence a partial order, on the natural numbers.


请参阅练习 1,了解可分性的一些基本性质。例如,对于每个ab, 和c, 如果abac然后ab+c,并且对于每一个ab, 和c, 如果ab然后abc.另外,如果abb0,则|a| 大于 |b|。我们将反复使用类似的属性。

See Exercise 1 for some basic properties of divisibility. For example, we have that for every a, b, and c, if ab and ac then ab+c, and for every a, b, and c, if ab then abc. Also, if ab and b0, then |a| le |b|. We will use properties like these repeatedly.

一个整数即使可以被2, 换句话说,n即使2n。整数如果不是偶数,则为奇数。当然,奇数的形式为2k+1对于一些k,现在我们就可以证明这一点。

An integer is even if it is divisible by 2, in other words, n is even if 2n. An integer is odd if it is not even. Of course, odd numbers are of the form 2k+1 for some k, and we can prove this now.


定理。如果n是一个奇数,那么n=2k+1对于某个整数k

Theorem. If n is an odd integer, then n=2k+1 for some integer k.

证明。根据商余数定理,我们可以写出n=2k+r对于某些整数kr0r<2最后一个条件意味着r=0或者r=1在第一种情况下,我们有n=2k, 因此2n,与之相矛盾n是奇数。所以我们有r=1,这意味着n=2k+1

Proof. By the quotient-remainder theorem, we can write n=2k+r for some integers k and r with 0r<2. The last condition means that r=0 or r=1. In the first case, we have n=2k, hence 2n, contradicting that n is odd. So we have r=1, which means that n=2k+1.

定理。每个序列k连续数字包含可被整除的数字k

Theorem. Every sequence of k consecutive numbers contains a number divisible by k.

证明。用以下公式表示序列中的最大元素:n. 这意味着序列是n(k1),,n1,n。根据商余数定理,我们有n=qk+r对于某些整数qr0r<k.从这些不等式中我们得出结论nr在我们的序列中,并且nr=qk,因此可以被整除k

Proof. Denote the largest element of the sequence by n. This means that the sequence is n(k1),,n1,n. By the quotient-remainder theorem, we have n=qk+r for some integers q and r with 0r<k. From these inequalities we conclude that nr is in our sequence, and nr=qk, hence divisible by k.


定义。给定两个整数mn如此一来m0或者n0,我们定义最大公约数 gcd(m,n)mn为最大整数d既是mn, 那是,dmdn

Definition. Given two integers m and n such that either m0 or n0, we define the greatest common divisor gcd(m,n) of m and n to be the largest integer d which is both a divisor of m and n, that is, dm and dn.

这个最大整数是存在的,因为至少有一个公约数,但只有有限个。至少有一个,因为 1 是任何两个整数的公约数,而有有限个,因为非零数只有有限个公约数。

This largest integer exists, because there is at least one common divisor, but only finitely many. There is at least one, since 1 is a common divisor of any two integers, and there are finitely many, since a nonzero number has only finitely many divisors.

如果n=m=0,然后我们定义gcd(0,0)=0

If n=m=0, then we define gcd(0,0)=0.


两个数的最大公约数始终是自然数,因为 1 始终是两个数的公约数。例如,让我们计算 6 和 28 的最大公约数。6 的正公约数是{1,2,3,6}28 的正因数是{1,2,4,7,14,28}这两个集合中最大的数字都是 2,它是 6 和 28 的最大公约数。

The greatest common divisor of two numbers is always a natural number, since 1 is always a common divisor of two numbers. As an example, let us compute the greatest common divisor of 6 and 28. The positive divisors of 6 are {1,2,3,6} and the positive divisors of 28 are {1,2,4,7,14,28}. The largest number in both these sets is 2, which is the greatest common divisor of 6 and 28.

但是,通过列出两个数的所有除数来计算两个数的最大公约数的工作量非常大,因此我们现在将考虑一种更有效地计算最大公约数的方法。

However, computing the greatest common divisor of two numbers by listing all the divisors of both numbers is a lot of work, so we will now consider a method to compute the greatest common divisor more efficiently.


引理对于所有整数mnk我们有gcd(m,n)=gcd(n,mkn)

Lemma. For all integers m, n and k we have gcd(m,n)=gcd(n,mkn).

证明。d=gcd(m,n)r=mkn。 如果m=n=0, 然后d=0=gcd(n,r),我们就完成了。

Proof. Let d=gcd(m,n) and r=mkn. If m=n=0, then d=0=gcd(n,r), and we’re done.

在另一种情况下,我们首先证明mnnr。为了看到这一点,让我们dmdn. 然后也dmkn通过练习 1。因此dnr. 另一方面,如果d是除数nr, 然后dr+kn, 因此dm, 因此dmn

In the other case we first show that the set of common divisors of m and n is the same as the set of the common divisors of n and r. To see this, let dm and dn. Then also dmkn by Exercise 1. Hence d is a common divisor of n and r. On the other hand, if d is a divisor of n and r, then dr+kn, hence dm, hence d is a common divisor of m and n.

由于公约数的集合相同,因此每个集合中的最大元素也相同,因此gcd(m,n)=gcd(n,mkn)

Since the sets of common divisors are the same, the largest element in each set is also the same, hence gcd(m,n)=gcd(n,mkn).

引理对于所有整数n我们有gcd(n,0)=|n|

Lemma. For all integers n we have gcd(n,0)=|n|.

证明。每个数字都是 0 的除数,因此最大公约数是n而 0 只是n,其绝对值n

Proof. Every number is a divisor of 0, hence the greatest common divisor of n and 0 is just the greatest divisor of n, which is the absolute value of n.


这两个引理为我们提供了一种快速计算两个数字的最大公约数的方法。这称为欧几里得算法。假设我们要计算gcd(m,n)

These two lemmas give us a quick way to compute the greatest common divisor of two numbers. This is called the Euclidean Algorithm. Suppose we want to compute gcd(m,n).

  • 我们让r0=mr1=n

  • We let r0=m and r1=n.

  • 鉴于riri+1我们计算ri+2作为的剩余部分ri当除以ri+1

  • Given ri and ri+1 we compute ri+2 as the remainder of of ri when divided by ri+1.

  • 一次ri=0,我们停下来,gcd(m,n)=|ri1|

  • Once ri=0, we stop, and gcd(m,n)=|ri1|.

这是可行的,因为根据上面的引理,我们有gcd(rk,rk+1)=gcd(rk+1,rk+2), 自从rk+2=rkqrk+1对于一些q. 因此如果ri=0我们有

This works, because by the lemmas above, we have gcd(rk,rk+1)=gcd(rk+1,rk+2), since rk+2=rkqrk+1 for some q. Hence if ri=0 we have

gcd(m,n)=gcd(r0,r1)=gcd(ri1,ri)=gcd(ri1,0)=|ri1|.

例如,假设我们要计算 1311 和 5757 的最大公约数。我们计算以下余数:

For example, suppose we want to compute the greatest common divisor of 1311 and 5757. We compute the following remainders:

5757=4×1311+5131311=2×513+285513=1×285+228285=1×228+57228=4×57+0.

因此gcd(1311,5757)=57。这比计算 1311 和 5757 的所有除数要快得多。

Hence gcd(1311,5757)=57. This is much quicker than computing all the divisors of both 1311 and 5757.

这是关于最大公约数的一个重要结果。由于历史原因,它被称为“引理”。

Here is an important result about greatest common divisors. It is only called a “lemma” for historical reasons.


定理(B‎ézout 引理)。设mn是整数。那么有整数ab使得am+bn=gcd(m,n)

Theorem (B‎ézout’s Lemma). Let m and n be integers. Then there are integers a and b such that am+bn=gcd(m,n).

证明。我们计算gcd(m,n)通过上面给出的欧几里得算法,在算法过程中我们得到中间值r0,r1,,rk在哪里rk=0.现在通过归纳i我们证明我们可以写ri=aim+bin对于某些整数aibi。 的确:r0=1m+0nr1=0m+1n.现在如果我们假设ri=aim+binri+1=ai+1m+bi+1n,我们知道ri+2=riqri+1, 在哪里qri当除以ri+1. 这些方程式合在一起给出

Proof. We compute gcd(m,n) by the Euclidean Algorithm given above, and during the algorithm we get the intermediate values r0,r1,,rk where rk=0. Now by induction on i we prove that we can write ri=aim+bin for some integers ai and bi. Indeed: r0=1m+0n and r1=0m+1n. Now if we assume that ri=aim+bin and ri+1=ai+1m+bi+1n, we know that ri+2=riqri+1, where q is the quotient of ri when divided by ri+1. These equations together give

ri+2=(aiqai+1)m+(biqbi+1)n.

这就完成了归纳。特别是,rk1=ak1m+bk1n,并且gcd(m,n)=±rk1我们可以写gcd(m,n)作为am+bn对于一些ab

This completes the induction. In particular, rk1=ak1m+bk1n, and since gcd(m,n)=±rk1 we can write gcd(m,n) as am+bn for some a and b.

替代证明。我们可以假设mn是正数,因为gcd(m,n)=gcd(|m|,|n|)。 让d是形式为的最小正数am+bn,即集合的最小元素{am+bna,bN}.我们声称d=gcd(m,n)

Alternative proof. We can assume m and n are positive, since gcd(m,n)=gcd(|m|,|n|). Let d be the least positive number of the form am+bn, that is, the smallest element of the set {am+bna,bN}. We claim d=gcd(m,n).

ab使得d=am+bn. 显然,如果cmcn, 然后cd. 因此足以证明dmdn.我们将证明dm,因为另一种情况是对称的。写为m=dq+r, 和0r<d我们需要展示r=0

Let a and b be such that d=am+bn. Clearly, if cm and cn, then cd. So it suffices to show dm and dn. We’ll show that dm, since the other case is symmetric. Write m=dq+r, with 0r<d. We need to show r=0.

我们有

We have

r=mdq=mq(am+bn)=(1aq)m+(qb)n,

r0r<d。 自从d是可以写成该形式的最小正数,我们有r=0。 因此m=dq, 所以dm

with r0 and r<d. Since d is the smallest positive number that can be written in that form, we have r=0. Hence m=dq, so dm.


推论。如果c是 的任何公约数mn, 然后cgcd(m,n)

Corollary. If c is any common divisor of m and n, then cgcd(m,n).

证明。根据 B‎ézout 引理,有ab使得gcd(m,n)=am+bn。 自从c两者相除mnc分裂am+bn根据下面的练习 1,因此gcd(m,n)

Proof. By B‎ézout’s Lemma, there are a and b such that gcd(m,n)=am+bn. Since c divides both m and n, c divides am+bn by Exercise 1 below, and hence also gcd(m,n).


特别令人感兴趣的是,除了 1 和1

Of special interest are pairs of integers which have no divisors in common, except 1 and 1.


定义。两个整数mn质,gcd(m,n)=1

Definition. Two integers m and n are coprime if gcd(m,n)=1.


命题。mnk为整数,使得mk互质。如果kmn然后kn

Proposition. Let m, n and k be integers such that m and k are coprime. If kmn then kn.

证明。根据 B‎ézout 引理,有ab使得am+bk=1. 乘以n给出amn+bkn=n自从k分裂mnk除以等式的左边,因此kn

Proof. By B‎ézout’s Lemma, there are a and b such that am+bk=1. Multiplying by n gives amn+bkn=n Since k divides mn, k divides the left-hand side of the equation, hence kn.


19.3.素数

19.3. Prime Numbers

在本节中,我们将考虑素数的性质。

In this section we consider properties of prime numbers.


定义。一个整数p2如果p为 1 且p. 一个整数n2非素数的叫做合数

Definition. An integer p2 is called prime if the only positive divisors of p are 1 and p. An integer n2 which is not prime is called composite.


素数的等效定义是具有恰好 2 个正因数的正数。

An equivalent definition of a prime number is a positive number with exactly 2 positive divisors.

回想一下第 17 章,每个大于 1 的自然数都可以写成素数的乘积。具体来说,每个大于 1 的自然数都可以被某个素数整除。

Recall from Chapter 17 that every natural number greater than 1 can be written as the product of primes. In particular, ever natural number greater than 1 is divisible by some prime number.

我们现在证明有关素数的一些其他性质。

We now prove some other properties about prime numbers.


定理。素数有无穷多个。

Theorem. There are infinitely many primes.

证明。为了矛盾,假设素数只有有限个p1,p2,,pk。 让n=p1×p2××pk。 自从n可以被整除pi面向所有人ik我们知道n+1不能被整除pi对于任意i。然而,我们假设这些都是质数,这与每个数字都能被质数整除的事实相矛盾。

Proof. Suppose for the sake of contradiction that there are only finitely many primes p1,p2,,pk. Let n=p1×p2××pk. Since n is divisible by pi for all ik we know that n+1 is not divisible by pi for any i. However, we assumed that these are all primes, contradicting the fact that every number is divisible by a prime number.

引理。如果n是一个整数,p是质数,那么np互质或pn

Lemma. If n is an integer and p is a prime number, then either n and p are coprime or pn.

证明。d=gcd(n,p)。 自从d是正除数p, 任何一个d=1或者d=p在第一种情况下,np根据定义,它们是互质的,在第二种情况下,我们有pn

Proof. Let d=gcd(n,p). Since d is a positive divisor of p, either d=1 or d=p. In the first case, n and p are coprime by definition, and in the second case we have pn.

命题。如果nm是整数,并且p是一个素数,pnm然后要么pn或者pm

Proposition. If n and m are integers and p is a prime number such that pnm then either pn or pm.

证明。假设pn。根据前面的引理,这意味着pn是互质的。由此我们可以得出pm

Proof. Suppose that pn. By the previous lemma, this means that p and n are coprime. From this we can conclude that pm.


本节的最后一个结果表明,素数是正整数乘法的“构建块”:所有其他整数都可以以本质上独特的方式写成素数的乘积。

The last result in this section captures that the primes are the “building blocks” of the positive integers for multiplication: all other integers can be written as a product of primes in an essentially unique way.


定理(算术基本定理)。设n>0是整数。那么有素数p1,,pk使得n=p1××pk。此外,这些素数在重新排序之前都是唯一的。这意味着,如果有素数q1,,q使得q1××q=n,然后qi是对pi准确地说,这意味着存在一个双射σ:{1,,k}{1,,k}使得qi=pσ(i)

Theorem (Fundamental Theorem of Arithmetic). Let n>0 be an integer. Then there are primes p1,,pk such that n=p1××pk. Moreover, these primes are unique up to reordering. That means that if there are prime numbers q1,,q such that q1××q=n, then the qi are a reordering of the pi. To be completely precise, this means that there is a bijection σ:{1,,k}{1,,k} such that qi=pσ(i).

备注: 1 可以写成零个素数的乘积。空乘积定义为 1。

Remark. 1 can be written as the product of zero prime numbers. The empty product is defined to be 1.

证明。我们已经看到,每个数字都可以写成素数的乘积,所以我们只需要证明重新排序之前的唯一性。假设这不成立,根据最小元素原理,让n是最小的正整数,使得n可以通过两种方式写成素数的乘积:n=p1××pk=q1××q

Proof. We have already seen that every number can be written as the product of primes, so we only need to prove the uniqueness up to reordering. Suppose this is not true, and by the least element principle, let n be the smallest positive integers such that n can be written as the product of primes in two ways: n=p1××pk=q1××q.

由于 1 只能写成素数的乘积,即空乘积,因此有n>1, 因此k1。 自从pk是素数,我们必须有pkqj对于一些j. 通过交换qjq,我们可以假设j=。 自从q也是素数,我们有pk=q

Since 1 can be written as product of primes only as an empty product, we have n>1, hence k1. Since pk is prime, we must have pkqj for some j. By swapping qj and q, we may assume that j=. Since q is also prime, we have pk=q.

现在我们有p1××pk1=q1××q1. 本产品小于n,但可以用两种不同的方式写成素数的乘积。但我们假设n是此类数字中最小的一个。矛盾!

Now we have p1××pk1=q1××q1. This product is smaller than n, but can be written as product of primes in two different ways. But we assumed n was the smallest such number. Contradiction!


19.4.模运算

19.4. Modular Arithmetic

在13.3 节讨论等价关系时,我们考虑了整数模等价关系的例子。这有时被认为是“时钟算术”。假设你有一个没有分针的 12 小时制时钟,所以它只有一根时针,可以指向 12、1、2、3、4、5、6、7、8、9、10、11 点,然后再次回到 12 点。我们可以用这个时钟做算术。

In the discussion of equivalence relations in Section 13.3 we considered the example of the relation of modular equivalence on the integers. This is sometimes thought of as “clock arithmetic.” Suppose you have a 12-hour clock without a minute hand, so it only has an hour hand which can point to the hours 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 and then it wraps to 12 again. We can do arithmetic with this clock.

  • 如果指针当前指向 10,那么 5 小时后它将指向 3。

  • If the hand currently points to 10, then 5 hours later it will point to 3.

  • 如果指针指向 7,那么在此之前的 23 小时,它指向 8。

  • If the hand points to 7, then 23 hours before that, it pointed to 8.

  • 如果指针指向 9 点,而我们工作了 8 个小时,那么当我们完成工作时,指针将指向 5 点。如果我们工作的时间是 9 点的两倍,那么指针将指向 1 点。

  • If the hand points to 9, and we work for a 8 hours, then when we are done the hand will point to 5. If we worked twice as long, starting at 9, the hand will point to 1.

我们希望用数学符号来写这些语句,这样我们就可以更容易地推理它们。我们不能写10+5=3对于第一个表达式,因为那将是错误的,所以我们使用符号10+53(mod12). 符号(mod12)表示我们忘记了 12 的倍数,我们使用带有三条水平线的“全等”符号来提醒我们这些值并不完全相等,而只能相等到 12 的倍数。另外两条线可以表示为7238(mod12)9+281(mod12)

We want to write these statements using mathematical notation, so that we can reason about them more easily. We cannot write 10+5=3 for the first expression, because that would be false, so instead we use the notation 10+53(mod12). The notation (mod12) indicates that we forget about multiples of 12, and we use the “congruence” symbol with three horizontal lines to remind us that these values are not exactly equal, but only equal up to multiples of 12. The other two lines can be formulated as 7238(mod12) and 9+281(mod12).

这里还有更多示例:

Here are some more examples:

  • 6+71(mod12)

  • 6+71(mod12)

  • 67426(mod12)

  • 67426(mod12)

  • 75351(mod12)

  • 75351(mod12)

最后一个例子表明我们也可以使用负数。

The last example shows that we can use negative numbers as well.

我们现在给出一个精确的定义。

We now give a precise definition.


定义。对于整数abn我们说ab全等 n如果nab. 这是写的ab(modn). 数量n称为模量

Definition. For integers a, b and n we say that a and b are congruent modulo n if nab. This is written ab(modn). The number n is called the modulus.


通常我们只在模量n是正数。

Typically we only use this definition when the modulus n is positive.


定理。模同余n是等价关系。

Theorem. Congruence modulo n is an equivalence relation.

证明。我们必须证明模同余n具有自反性、对称性和传递性。

Proof. We have to show that congruence modulo n is reflexive, symmetric and transitive.

它是反身性的,因为aa=0, 所以naa,因此aa(modn)

It is reflexive, because aa=0, so naa, and hence aa(modn).

为了证明它是对称的,假设ab(modn).那么根据定义,nab。 所以n(1)(ab),这意味着nba. 这意味着ba(modn)

To show that it is symmetric, suppose that ab(modn). Then by definition, nab. So n(1)(ab), which means that nba. This means by definition that ba(modn).

为了证明它是传递的,假设ab(modn)bc(modn).然后我们有nabnbc。因此我们有n(ab)+(bc)这意味着nac。 所以ac(modn)

To show that it is transitive, suppose that ab(modn) and bc(modn). Then we have nab and nbc. Hence we have n(ab)+(bc) which means that nac. So ac(modn).


这个定理证明了我们上面使用的“链式”符号的合理性,我们这样写75351(mod12)。由于模 12 的一致性是传递性的,我们现在可以得出这样的结论:751(mod12)

This theorem justifies the “chaining” notation we used above when we wrote 75351(mod12). Since congruence modulo 12 is transitive, we can now actually conclude that 751(mod12).


定理。假设ab(modn)cd(modn)。 然后a+cb+d(modn)acbd(modn)

Theorem. Suppose that ab(modn) and cd(modn). Then a+cb+d(modn) and acbd(modn).

此外,如果ab(modn)然后akbk(modn)对于所有自然数k

Moreover, if ab(modn) then akbk(modn) for all natural numbers k.

证明。我们知道nabncd对于第一个语句,我们可以计算出(a+c)(b+d)=(ab)+(cd),因此我们可以得出结论n(a+c)(b+d)因此a+cb+d(modn)

Proof. We know that nab and ncd. For the first statement, we can calculate that (a+c)(b+d)=(ab)+(cd), so we can conclude that n(a+c)(b+d) hence that a+cb+d(modn).

对于第二条陈述,我们想证明nacbd我们可以分解acbd=(ab)c+b(cd)。 现在n将右边的两个加数相除,因此n分裂acbd,这意味着acbd(modn)

For the second statement, we want to show that nacbd. We can factor acbd=(ab)c+b(cd). Now n divides both summands on the right, hence n divides acbd, which means that acbd(modn).

最后一句话是归纳k。 如果k=0, 然后11(modn),对于诱导步骤,假设akbk(modn),那么我们有ak+1=aakbbk=bk+1(modn)

The last statement follows by induction on k. If k=0, then 11(modn), and for the induction step, suppose that akbk(modn), then we have ak+1=aakbbk=bk+1(modn).


该定理对于进行模计算很有用n。下面是一些例子。

This theorem is useful for carrying out computations modulo n. Here are some examples.

  • 假设我们要计算77123模 12。我们知道775(mod12)1233(mod12), 所以7712353153(mod12)

  • Suppose we want to compute 77123 modulo 12. We know that 775(mod12) and 1233(mod12), so 7712353153(mod12)

  • 假设我们要计算99998模 10。我们知道991(mod10)9982(mod10), 因此99998(1)(2)2(mod10)

  • Suppose we want to compute 99998 modulo 10. We know that 991(mod10) and 9982(mod10), hence 99998(1)(2)2(mod10).

  • 假设我们想知道101101。请注意,数字的最后一位n符合于n模 10,所以我们可以计算10110111011(mod10). 因此101101为 1。

  • Suppose we want to know the last digit of 101101. Notice that the last digit of a number n is congruent to n modulo 10, so we can just compute 10110111011(mod10). So the last digit of 101101 is 1.

警告。您无法使用模块化算法完成所有预期的计算:

Warning. You cannot do all computations you might expect with modular arithmetic:

  • 模数运算不考虑除法。例如1216(mod4),但我们不能把等式的两边同时除以 2,因为68(mod4)

  • Modular arithmetic does not respect division. For example 1216(mod4), but we cannot divide both sides of the equation by 2, because 68(mod4).

  • 指数也不遵循模运算。例如83(mod5), 但2823(mod5). 要查看此内容:28=2561(mod5), 但23=83(mod5)

  • Exponents also do not respect modular arithmetic. For example 83(mod5), but 2823(mod5). To see this: 28=2561(mod5), but 23=83(mod5).

回想一下商余数定理:如果n>0,然后任意整数a可以表示为a=nq+r, 在哪里0r<n. 在模运算语言中,这意味着ar(modn). 因此如果n>0,则每个整数都与 0 到n1(含)。因此“只有n取模运算时“不同的数字”n这可以用来证明许多关于自然数的命题。

Recall the quotient-remainder theorem: if n>0, then any integer a can be expressed as a=nq+r, where 0r<n. In the language of modular arithmetic this means that ar(modn). So if n>0, then every integer is congruent to a number between 0 and n1 (inclusive). So there “are only n different numbers” when working modulo n. This can be used to prove many statements about the natural numbers.


命题。对于每个整数kk2+1不能被 3 整除。

Proposition. For every integer k, k2+1 is not divisible by 3.

证明。将这个问题转化为模运算,我们必须证明k2+10(mod3)或者换句话说k22(mod3)面向所有人k。根据商余数定理,我们知道k与 0、1 或 2 模 3 一致。在第一种情况下,k2020(mod3).在第二种情况下,k2121(mod3),在最后一种情况下,我们有k22241(mod3). 在所有这些情况下,k22(mod3)。 所以k2+1永远不能被 3 整除。

Proof. Translating this problem to modular arithmetic, we have to show that k2+10(mod3) or in other words that k22(mod3) for all k. By the quotient-remainder theorem, we know that k is either congruent to 0, 1 or 2, modulo 3. In the first case, k2020(mod3). In the second case, k2121(mod3), and in the last case we have k22241(mod3). In all of those cases, k22(mod3). So k2+1 is never divisible by 3.


命题。对于所有整数aba2+b23不能被 4 整除。

Proposition. For all integers a and b, a2+b23 is not divisible by 4.

证明。我们首先计算模 4 的平方。我们计算

Proof. We first compute the squares modulo 4. We compute

020(mod4)121(mod4)220(mod4)321(mod4).

因为每个数都与 0、1、2 或 3 模 4 一致,所以我们知道每个平方数都与 0 或 1 模 4 一致。这意味着a2+b2(mod4). 它可以等同于0+01+00+1或者1+1. 在所有这些情况下,a2+b23(mod4)因此4a2+b23,证明了命题。

Since every number is congruent to 0, 1, 2 or 3 modulo 4, we know that every square is congruent to 0 or 1 modulo 4. This means that there are only four possibilities for a2+b2(mod4). It can be congruent to 0+0, 1+0, 0+1 or 1+1. In all those cases, a2+b23(mod4) Hence 4a2+b23, proving the proposition.


回想一下,我们警告过你关于模运算中的除法。这并不总是有效,但通常有效。例如,假设我们想解决2n1(mod5)我们不能通过说n12(mod5),因为我们不能在模运算中处理分数。但是,我们仍然可以通过将两边乘以 3 来解决这个问题。然后我们得到6n3(mod5),并且61(mod5)我们得到n3(mod5)。因此,我们可以乘以 3 来得到答案,而不是除以 2。这样做的原因是2×31(mod5)

Recall that we warned you about dividing in modular arithmetic. This doesn’t always work, but often it does. For example, suppose we want to solve 2n1(mod5). We cannot solve this by saying that n12(mod5), because we cannot work with fractions in modular arithmetic. However, we can still solve it by multiplying both sides with 3. Then we get 6n3(mod5), and since 61(mod5) we get n3(mod5). So instead of dividing by 2 we could multiply by 3 to get the answer. The reason this worked is because 2×31(mod5).


定义。na是整数 a 模数 n是一个整数b使得ab1(modn)

Definition. Let n and a be integers. A multiplicative inverse of a modulo n is an integer b such that ab1(modn).


例如,3 是 5 模 7 的乘法逆元,因为3×51(mod7)。 但2没有模 6 的乘法逆元。实际上,假设2b1(mod6), 然后62b1。 然而,2b1是奇数,不能被偶数整除。我们可以使用乘法逆元来解方程。如果我们想解axc(modn)为了x我们知道ba,解决方案是xbc(modn)我们可以通过同时乘以b

For example, 3 is a multiplicative inverse of 5 modulo 7, since 3×51(mod7). But 2 has no multiplicative inverse modulo 6. Indeed, suppose that 2b1(mod6), then 62b1. However, 2b1 is odd, and cannot be divisible by an even number. We can use multiplicative inverses to solve equations. If we want to solve axc(modn) for x and we know that b is a multiplicative inverse of a, the solution is xbc(modn) which we can see by multiplying both sides by b.


引理na为整数。a最多有一个乘法逆模n也就是说,如果bb都是a模数n, 然后bb(modn)

Lemma Let n and a be integers. a has at most one multiplicative inverse modulo n. That is, if b and b are both multiplicative inverses of a modulo n, then bb(modn).

证明。假设ab1ab(modn).然后我们可以计算bab有两种方式:bb(ab)=(ba)bb(modn)

Proof. Suppose that ab1ab(modn). Then we can compute bab in two ways: bb(ab)=(ba)bb(modn).

命题。na为整数。a具有乘法逆模n当且仅当na是互质的。

Proposition. Let n and a be integers. a has a multiplicative inverse modulo n if and only if n and a are coprime.

证明。假设ba模数n。 然后nab1。 让d=gcd(a,n)。 自从dn我们有dab1.但自从d是除数ab,我们有dab(ab1)=1。 自从d0我们有d=1。 因此na是互质的。

Proof. Suppose b is a multiplicative inverse of a modulo n. Then nab1. Let d=gcd(a,n). Since dn we have dab1. But since d is a divisor of ab, we have dab(ab1)=1. Since d0 we have d=1. Hence n and a are coprime.

另一方面,假设na是互质的。根据贝祖引理,我们知道有整数bc使得cn+ba=gcd(n,a)=1我们可以将其重写为ab1=(c)n, 因此nab1,根据定义,这意味着ab1(modn).这意味着ba模数n

On the other hand, suppose that n and a are coprime. By B‎ézout’s Lemma we know that there are integers b and c such that cn+ba=gcd(n,a)=1. We can rewrite this to ab1=(c)n, hence nab1, which means by definition ab1(modn). This means that b is a multiplicative inverse of a modulo n.


请注意,如果p是质数,a是不能被整除的整数p, 然后ap互质,因此a具有乘法逆元。

Note that if p is a prime number and a is a integer not divisible by p, then a and p are coprime, hence a has a multiplicative inverse.

19.5.正方形的性质

19.5. Properties of Squares

自古以来,数学家们就对哪些整数可以写成两个平方和的问题很感兴趣。例如,我们可以写成2=11+115=22+1213=32+22。如果我们列出足够长的列表,就会出现一个有趣的模式:如果两个数字可以写成两个平方和,那么它们的乘积也可以。例如,10=52,我们可以写10=32+12。 或者65=135,我们可以写65=82+12

Mathematicians from ancient times have been interested in the question as to which integers can be written as a sum of two squares. For example, we can write 2=11+11, 5=22+12, 13=32+22. If we make a sufficiently long list of these, an interesting pattern emerges: if two numbers can be written as a sum of two squares, then so can their product. For example, 10=52, and we can write 10=32+12. Or 65=135, and we can write 65=82+12.

一开始,你可能会怀疑这是否只是巧合。下面将提供证据来证明事实并非如此。

At first, one might wonder whether this is just a coincidence. The following provides a proof of the fact that it is not.


定理。xy是任意两个整数。如果xy都是两个平方和,那么xy

Theorem. Let x and y be any two integers. If x and y are both sums of two squares, then so is xy.

证明。假设x=a2+b2,假设y=c2+d2.我认为

Proof. Suppose x=a2+b2, and suppose y=c2+d2. I claim that

xy=(acbd)2+(ad+bc)2.

为了说明这一点,请注意,一方面我们有

To show this, notice that on the one hand we have

xy=(a2+b2)(c2+d2)=a2c2+a2d2+b2c2+b2d2.

另一方面,我们有

On the other hand, we have

(acbd)2+(ad+bc)2=(a2c22abcd+b2d2)+(a2d2+2abcd+b2c2)=a2c2+b2d2+a2d2+b2c2.

就加数的顺序而言,两个右边是相同的。

Up to the order of summands, the two right-hand sides are the same.


考虑素数,2,3,5,7,11,13,。哪些可以写成两个平方数之和?我们有2=12+125=22+12, 和13+32+22。尝试所有可能性表明 3、7 和 11 不能写成两个平方数之和。请注意,任何奇素数都与 1 或 3 模 4 一致。费马有一个很棒的定理(我们不会在这里证明),它表明奇素数可以写成平方数之和,当且仅当它与 1 模 4 一致。

Consider the prime numbers, 2,3,5,7,11,13,. Which ones can be written as sums of two squares? We have 2=12+12, 5=22+12, and 13+32+22. Trying all possibilities shows that 3, 7, and 11 cannot be written as sums of two squares. Notice that any odd prime is congruent to either 1 or 3 modulo 4. A lovely theorem by Fermat, which we will not prove here, shows that an odd prime can be written as a sum of squares if and only if it is congruent to 1 modulo 4.

我们现在将证明2不是两个整数的分数。

We will now prove that 2 is not a fraction of two integers.


定理。没有整数ab使得ab=2

Theorem. There are no integers a and b such that ab=2.

证明。假设ab=2对于某些整数ab。通过取消共同因素,我们可以假设ab互质。通过对两边求平方,我们得到a2b2=2,并将两边乘以b2给出a2=2b2。 自从2b2是偶数,我们知道a2是偶数,又因为奇数的平方数是奇数,所以我们得出a是偶数。因此我们可以写a=2c对于某个整数c.这意味着(2c)2=2b2, 因此2c2=b2. 同样的推理表明b是偶数。但我们假设ab是互质的,这与它们都是偶数的事实相矛盾。

Proof. Suppose that ab=2 for some integers a and b. By canceling common factors, we may assume that a and b are coprime. By squaring both sides, we get a2b2=2, and multiplying both sides by b2 gives a2=2b2. Since 2b2 is even, we know that a2 is even, and since odd squares are odd, we conclude that a is even. Hence we can write a=2c for some integer c. This means that (2c)2=2b2, hence 2c2=b2. The same reasoning shows that b is even. But we assumed that a and b are coprime, which contradicts the fact that they are both even.

因此没有整数ab使得ab=2

Hence there are no integers a and b such that ab=2.


19.6.练习

19.6. Exercises

  1. 证明以下关于可整除性的性质(对于任何整数abc):

    • 如果abac然后ab+cabc

    • 如果ab然后abc

    • a0

    • 如果0a然后a=0

    • 如果a0然后是语句bcabac是等效的。

    • 如果abb0然后|a||b|

  2. Prove the following properties about divisibility (for any integers a, b and c):

    • If ab and ac then ab+c and abc.

    • If ab then abc.

    • a0;

    • If 0a then a=0.

    • If a0 then the statements bc and abac are equivalent.

    • If ab and b0 then |a||b|.

  3. 证明如果k0km, 和kn, 然后gcd(m/k,n/k)=gcd(m,n)/k(提示:这有助于表明,无论何时a0ab, 和bc, 然后b/ac/a

  4. Prove that if k0, km, and kn, then gcd(m/k,n/k)=gcd(m,n)/k. (Hint: it helps to show that whenever a0, ab, and bc, then b/ac/a.)

  5. 证明对于任何整数nn2除以 4 后余数为 0 或 1。得出以下结论n2+2永远不能被 4 整除。

  6. Prove that for any integer n, n2 leaves a remainder of 0 or 1 when you divide it by 4. Conclude that n2+2 is never divisible by 4.

  7. 证明如果n很奇怪,n21可以被 8 整除。

  8. Prove that if n is odd, n21 is divisible by 8.

  9. 证明如果mn是奇数,那么m2+n2是偶数但不能被 4 整除。

  10. Prove that if m and n are odd, then m2+n2 is even but not divisible by 4.

  11. 如果两个整数都是偶数或都是奇数,则称它们“具有相同的奇偶性”。证明如果mn是任意两个整数,那么m+nmn具有相同的奇偶校验。

  12. Say that two integers “have the same parity” if they are both even or both odd. Prove that if m and n are any two integers, then m+n and mn have the same parity.

  13. 将 11160 写为质数的乘积。

  14. Write 11160 as a product of primes.

  15. 列出 42 和 198 的所有因数,然后通过查看两个列表中的最大数字来找到最大公约数。还使用欧几里得算法计算数字的最大公约数。

  16. List all the divisors of 42 and 198, and find the greatest common divisor by looking at the largest number in both lists. Also compute the greatest common divisor of the numbers by the Euclidean Algorithm.

  17. 计算gcd(15,55)gcd(12345,54321)gcd(77,110)

  18. Compute gcd(15,55), gcd(12345,54321) and gcd(77,110)

  19. 通过感应显示n对于每一对整数xyxy分裂xnyn.(提示:在归纳步骤中,写下xn+1yn+1作为xn(xy)+xnyyn+1

  20. Show by induction on n that for every pair of integers x and y, xy divides xnyn. (Hint: in the induction step, write xn+1yn+1 as xn(xy)+xnyyn+1.)

  21. 计算212(mod13). 使用它来计算21212004(mod13)

  22. Compute 212(mod13). Use this to compute 21212004(mod13).

  23. 查找最后一位数字9999。您还能找到这个数字的最后两位数字吗?

  24. Find the last digit of 9999. Can you also find the last two digits of this number?

  25. 证明50222250可以被 7 整除。

  26. Prove that 50222250 is divisible by 7.

  27. 检查以下乘法逆元是否存在,如果存在,则找出它们。

    • 5 模 7 的乘法逆元

    • 17 模 21 的乘法逆元

    • 4 模 14 的乘法逆元

    • 的乘法逆元2模 9

  28. Check whether the following multiplicative inverses exist, and if so, find them.

    • the multiplicative inverse of 5 modulo 7

    • the multiplicative inverse of 17 modulo 21

    • the multiplicative inverse of 4 modulo 14

    • the multiplicative inverse of 2 modulo 9

  29. 查找所有整数x使得75x45(mod8)

  30. Find all integers x such that 75x45(mod8).

  31. 证明对于每个整数n号码n4与 0 或 1 模 5 一致。提示:为了简化计算,使用44(1)4(mod5)

  32. Show that for every integer n the number n4 is congruent to 0 or 1 modulo 5. Hint: to simplify the computation, use that 44(1)4(mod5).

  33. 证明方程n4+m4=k4+3在整数中没有解。(提示:使用上一个练习。)

  34. Prove that the equation n4+m4=k4+3 has no solutions in the integers. (Hint: use the previous exercise.)

  35. 认为p是一个素数,pk. 证明如果knkm(modp)然后nm(modp)

  36. Suppose p is a prime number such that pk. Show that if knkm(modp) then nm(modp).

  37. nmc给定整数。使用 B‎ézout 引理证明方程an+bm=c有整数解ab当且仅当gcd(n,m)c

  38. Let n, m and c be given integers. Use B‎ézout’s Lemma to prove that the equation an+bm=c has a solution for integers a and b if and only if gcd(n,m)c.

  39. 假设anam并让d=gcd(n,m). 证明gcd(na,ma)=da. 得出以下结论:对于任何两个整数nm最大公约数d数字ndmd是互质的。

  40. Suppose that an and am and let d=gcd(n,m). Prove that gcd(na,ma)=da. Conclude that for any two integers n and m with greatest common divisor d the numbers nd and md are coprime.

20.组合学

20. Combinatorics

组合学是一门无需计数的计数艺术。确定给定集合中有多少东西是一项基本的数学任务,当集合很大时,逐个计算元素可能会很繁琐或不可行。此外,当用变化的参数(例如自然数)来描述集合时,n),我们希望有一个公式告诉我们对象的数量如何取决于该参数。在本章中,我们将为实现这一目标奠定基础,并学习一些诀窍。

Combinatorics is the art of counting without counting. It is a fundamental mathematical task to determine how many things there are in a given collection, and when the collection is large, it can be tedious or infeasible to count the elements individually. Moreover, when the collection is described in terms of a changing parameter (say, a natural number, n), we would like a formula that tells us how the number of objects depends on that parameter. In this chapter we will set up a foundation for achieving this goal, and learn some of the tricks of the trade.

20.1.有限集和基数

20.1. Finite Sets and Cardinality

对于每个自然数来说,这都是有帮助的n,有一个大小为n为此,我们将选择

It will be helpful, for every natural number n, to have a canonical set of elements of size n. To that end, we will choose the set

[n]={mm<n}={0,1,,n1}.

我们使用了相同的符号,[n],描述关于等价关系的等价类,但希望我们想要表达的意思从上下文中总能很清楚。

We used the same notation, [n], to describe equivalence classes with respect to an equivalence relation, but hopefully our intended meaning will always be clear from the context.

一套A如果存在一个双射,则元素的数量称为有限的[n]A对于一些n。在这种情况下,我们想说A n 元素,或者集合A 具有基数 n,并写|A|=n。但要做到这一点,我们需要知道什么时候A是有限的,有一个独特的n具有上述属性。

A set A of elements is said to be finite if there is a bijection from [n] to A for some n. In that case, we would like to say that A has n elements, or that the set A has cardinality n, and write |A|=n. But to do so, we need to know that when A is finite, there is a unique n with the property above.

假设两者都有双射[m][n]A. 将第一个双射与第二个双射的逆组合起来,我们得到一个双射[m][n]. 直观上看,这似乎意味着m=n,但我们的目标是从集合、函数和自然数的基本性质来证明这一点。

Suppose there are bijections from both [m] and [n] to A. Composing the first bijection with the inverse of the second, we get a bijection from [m] to [n]. It seems intuitively clear that this implies m=n, but our goal is to prove this from the fundamental properties of sets, functions, and the natural numbers.

因此,为了矛盾,假设mn. 不失一般性,我们可以假设m>n(为什么?)特别是,有一个注入函数f[m][n]。 自从m>nmn+1,因此我们可以限制f从中获取注入函数[n+1][n]。下一个定理表明这不可能发生。

So suppose, for the sake of contradiction, mn. Without loss of generality, we can assume m>n (why?). In particular, there is an injective function f from [m] to [n]. Since m>n, mn+1, and so we can restrict f to get an injective function from [n+1] to [n]. The next theorem shows that this cannot happen.


定理。对于任何自然数n,没有来自[n+1][n]

Theorem. For any natural number n, there is no injective function from [n+1] to [n].

证明。通过归纳n. 当n=0, 因为[1]={0}[0]=。 如果f是来自的注入函数[1][0],我们会f(0),这是不可能的。

Proof. By induction on n. The theorem is clear when n=0, because [1]={0} and [0]=. If f were an injective function from [1] to [0], we would have f(0), which is impossible.

假设断言对n,假设f是一个来自的注入函数[n+2][n+1].我们考虑两种情况。

So suppose the claim is true for n, and suppose f is an injective function from [n+2] to [n+1]. We consider two cases.

在第一种情况下,假设n不是f。 然后f地图[n+2][n],并限制域,我们有一个来自的注入函数[n+1][n],与归纳假设相矛盾。

In the first case, suppose n is not in the image of f. Then f maps [n+2] to [n], and restricting the domain, we have an injective function from [n+1] to [n], contradicting the inductive hypothesis.

在第二种情况下,m<n+2使得f(m)=n. 这个想法是改变f稍微得到一个注入函数[n+1][n],再次与归纳假设相矛盾。如果m=n+1,也就是说它是[n+2]映射到的最后一个元素[n+1],我们可以限制f[n+1]. 事实是f是单射意味着[n+1]映射到n

In the second case, there is some m<n+2 such that f(m)=n. The idea is to alter f slightly to get an injective function from [n+1] to [n], again contradicting the inductive hypothesis. If m=n+1, which is to say it is the last element of [n+2] that is mapped to the last element of [n+1], we can just restrict f to [n+1]. The fact that f was injective implies that all the elements in [n+1] are mapped to n.

否则,定义f:[n+1][n]经过

Otherwise, define f:[n+1][n] by

f(i)={f(i)if imf(n+1)if i=m.

换句话说,我们映射m达到n+1被映射到。由于f是单射,f(n+1)f(m), 所以f(n+1)<n,如要求的那样。不难检查f是单射,所以我们得到了我们想要的矛盾。

In other words, we map m to the value that n+1 was mapped to. Since f is injective, f(n+1)f(m), and so f(n+1)<n, as required. It is not hard to check that f is injective, so we have the contradiction we were after.


这个定理被称为“鸽巢原理”。这意味着如果n+1鸽子栖息n洞,那么至少有一个洞里有不止一只鸽子。该原理意味着对于每个有限集A,有一个独特的n这样就存在一个双射[n]A,我们可以定义基数A成为那个n

This theorem is known as the “pigeonhole principle.” It implies that if n+1 pigeons inhabit n holes, then at least one hole has more than one pigeon. The principle implies that for every finite set A, there is a unique n such that there is a bijection from [n] to A, and we can define the cardinality of A to be that n.

我们现在引入符号iAf(i)iAf(i)对于有限集上的和与积。如果A={a0,,an1}, 然后iAf(i)定义为f(a0)++f(an1),对于乘积也是如此。正式地,我们所做的是选择一个双射g:[n]A并定义iAf(i)j<nf(g(j))。需要做一些工作来证明这是有道理的,也就是说,我们得到的答案并不取决于我们选择哪个双射。我们在这里就认为这个事实是理所当然的。

We now introduce the notation iAf(i) and iAf(i) for sums and products over finite sets. If A={a0,,an1}, then iAf(i) is defined to be f(a0)++f(an1), and similarly for products. Formally, what we are doing is choosing a bijection g:[n]A and defining iAf(i) to be j<nf(g(j)). It takes some work to show that this makes sense, which is to say, the answer we get doesn’t depend on which bijection we choose. We will just take this fact for granted here.

20.2.计数原则

20.2. Counting Principles

这是一个基本的计数原理。

Here is a basic counting principle.


定理。AB是不相交的有限集。然后|AB|=|A|+|B|

Theorem. Let A and B be disjoint finite sets. Then |AB|=|A|+|B|.

证明。假设f:[m]Ag:[n]B是双射。定义h:[m+n]AB经过

Proof. Suppose f:[m]A and g:[n]B are bijections. Define h:[m+n]AB by

h(i)={f(i)if i<mg(im)if mi<m+n.

看到这一点h是全射,注意每一个kAB可以写成k=f(i)对于一些i[m]或者k=g(j)对于一些j[n]在第一种情况下,k=f(i)=h(i),在第二种情况下,k=g(j)=h(m+j)

To see that h is surjective, note that every k in AB can be written as either k=f(i) for some i[m] or k=g(j) for some j[n]. In the first case, k=f(i)=h(i), and in the second case, k=g(j)=h(m+j).

不难证明h也是单射。假设h(i)=h(j)。 如果h(i)位于A,那么它就不在范围内g,所以我们必须h(i)=f(i)h(j)=f(j)。 然后f(i)=f(j),注入性f意味着i=j。 如果h(i)而是B,论点也类似。

It is not hard to show that h is also injective. Suppose h(i)=h(j). If h(i) is in A, then it is not in the range of g, and so we must have h(i)=f(i) and h(j)=f(j). Then f(i)=f(j), the injectivity of f implies that i=j. If h(i) is instead in B, the argument it similar.


证明只是阐明了我们的基本直觉:如果你想列出AB,你可以列出所有元素A然后所有元素B.如果AB没有共同的元素,那么计算元素AB,你可以计算元素A然后继续计算元素B一旦您可以轻松地将直观的论证转化为精确的数学证明(数学家通常都能做到这一点),您就可以使用更直观的描述(数学家通常都会这样做)。

The proof only spells out our basic intuitions: if you want to list all of the elements of AB, you can list all the elements of A and then all the elements of B. And if A and B have no elements in common, then to count the elements of AB, you can count the elements of A and then continue counting the elements of B. Once you are comfortable translating the intuitive argument into a precise mathematical proof (and mathematicians generally are), you can use the more intuitive descriptions (and mathematicians generally do).

这是另一个基本的计数原则:

Here is another basic counting principle:


定理。AB是有限集。然后|A×B|=|A||B|

Theorem. Let A and B be finite sets. Then |A×B|=|A||B|.


注意,这次我们计算的是有序对的数量(a,b)aAbB练习要求你给出这个定理的详细证明。至少有两种方法可以做到这一点。第一种方法是从双射开始f:[m]Ag:[n]B并描述一个明确的双射h:[mn]A×B.二是修复m说,并使用归纳法n和之前的计数原则。注意如果UV是任意集合并且w不在V,我们有

Notice that this time we are counting the number of ordered pairs (a,b) with aA and bB. The exercises ask you to give a detailed proof of this theorem. There are at least two ways to go about it. The first is to start with bijections f:[m]A and g:[n]B and describe an explicit bijection h:[mn]A×B. The second is to fix m, say, and use induction on n and the previous counting principle. Notice that if U and V are any sets and w is not in V, we have

U×(V{w})=(U×V)(U×{w}),

并且此并集内的两组集合是不相交的。

and the two sets in this union are disjoint.

正如我们有联盟的概念iIAi和交点iIAi对于索引集合族,有一个乘积的概念很有用iIAi我们可以想到一个元素a该产品作为一个函数,对于每个元素iI,返回一个元素aiAi。例如,当I={1,2,3},元素iIAi只是一个三元组a1,a2,a3a1A1a2A2, 和a3A3. 这本质上等同于A1×A2×A3至于我们是否将三元组表示为函数或迭代配对,则取决于一些复杂细节(a1,(a2,a3))

Just as we have notions of union iIAi and intersection iIAi for indexed families of sets, it is useful to have a notion of a product iIAi. We can think of an element a of this product as a function which, for each element iI, returns an element aiAi. For example, when I={1,2,3}, an element of iIAi is just a triple a1,a2,a3 with a1A1, a2A2, and a3A3. This is essentially the same as A1×A2×A3, up to the fiddly details as to whether we represent a triple as a function or with iterated pairing (a1,(a2,a3)).


定理。I是一个有限指标集,并让(Ai)iI是一组有限集。然后:

Theorem. Let I be a finite index set, and let (Ai)iI be a family of finite sets. Then:

  • 如果每对集合AiAj是不相交的,那么|iIAi|=iI|Ai|

  • If each pair of sets Ai, Aj are disjoint, then |iIAi|=iI|Ai|.

  • |iIAi|=iI|Ai|

  • |iIAi|=iI|Ai|.

证明。通过归纳|I|,使用先前的计数原理。

Proof. By induction on |I|, using the previous counting principles.


我们已经可以利用这些原理进行基本的计算。

We can already use these principles to carry out basic calculations.


示例。一家餐厅的甜点菜单上有四种口味的冰淇淋、两种蛋糕和三种馅饼。有多少种甜点可供选择?

Example. The dessert menu at a restaurant has four flavors of ice cream, two kinds of cake, and three kinds of pie. How many dessert choices are there?

解决方案。 4+2+3=9,三个不相交集合的并集的基数。

Solution. 4+2+3=9, the cardinality of the union of the three disjoint sets.

示例。一家餐厅的菜单上有 6 种开胃菜、7 种主菜和 5 种甜点。那么三道菜的晚餐有多少种选择?

Example. The menu at a diner has 6 choices of appetizers, 7 choices of entrée, and 5 choices of dessert. How many choices of three-course dinners are there?

解决方案。三道菜的晚餐是由开胃菜、主菜和甜点组成的三重菜。因此675=210选项。

Solution. A three-course dinner is a triple consisting of an appetizer, an entrée, and a dessert. There are therefore 675=210 options.


当所有集合的大小相同时,会出现上述计数原理的一个特殊情况。如果I具有基数k以及每个Ai具有基数n,那么基数iIAikn如果集合是两两不相交的,并且iIAink

A special case of the previous counting principles arises when all the sets have the same size. If I has cardinality k and each Ai has cardinality n, then the cardinality of iIAi is kn if the sets are pairwise disjoint, and the cardinality of iIAi is nk.


示例。一副扑克牌有四种花色(方块、红心、黑桃和梅花),每种花色有 13 张牌,总共413=52

Example. A deck of playing cards has four suits (diamonds, hearts, spades, and clubs) and 13 cards in each suit, for a total of 413=52.

示例。长度为n是一系列n许多 0 和 1。我们可以将其视为

Example. A binary string of length n is a sequence of n many 0’s and 1’s. We can think of this as an element of

{0,1}n=i<n{0,1},

所以有2n许多长度的二进制字符串n

so there are 2n many binary strings of length n.


还有另一条显而易见的计算原则:如果A是一个有限集,并且之间存在双射AB, 然后B也是有限的,并且|A|=|B|

There is another counting principle that is almost too obvious to mention: if A is a finite set and there is a bijection between A and B, then B is also finite, and |A|=|B|.


例子。考虑[n],即{0,1,2,,n1}子集与长度为的二进制字符串之间存在一一对应关系n,其中元素i字符串是1如果i在集合中,并且0否则。因此,我们有|P([n])|=2n

Example. Consider the power set of [n], that is, the collection of all subsets of {0,1,2,,n1}. There is a one-to-one correspondence between subsets and binary strings of length n, where element i of the string is 1 if i is in the set and 0 otherwise. As a result, we have |P([n])|=2n.


20.3.有序选择

20.3. Ordered Selections

S是一个有限集,我们将其视为一组选项,例如菜单上的项目或可以从书架上选择的书籍。现在我们来讨论组合学中的一类问题,这些问题涉及从该选项集中进行重复选择。在每种情况下,都有有限多个选择,并且顺序很重要:有第一个选择、第二个选择、第三个选择等等。

Let S be a finite set, which we will think of as being a set of options, such as items on a menu or books that can be selected from a shelf. We now turn to a family of problems in combinatorics that involves making repeated selections from that set of options. In each case, there are finitely many selections, and the order counts: there is a first choice, a second one, a third one, and so on.

在问题的第一个变体中,你可以重复选择。例如,如果你从 31 种冰淇淋口味列表中选择 3 种口味,你可以选择“巧克力、香草、巧克力”。这被称为重复有序选择。如果你正在做k选择n中的选项S,这样的选择本质上是一个元组(a0,a1,,ak1),其中每个ai是其中之一n中的元素S换句话说,k选自S重复是集合Sk,我们在上一节中已经看到,如果S具有基数n,集合Sk具有基数nk

In the first variant of the problem, you are allowed to repeat a choice. For example, if you are choosing 3 flavors from a list of 31 ice cream flavors, you can choose “chocolate, vanilla, chocolate.” This is known as ordered selection with repetition. If you are making k choices from among n options in S, such a selection is essentially a tuple (a0,a1,,ak1), where each ai is one of the n elements in S. In other words, the set of ways of making k selections from S with repetition is the set Sk, and we have seen in the last section that if S has cardinality n, the set Sk has cardinality nk.


定理。S是一组n元素。然后制作方法的数量k选自S允许重复的是nk

Theorem. Let S be a set of n elements. Then the number of ways of making k selections from S with repetition allowed is nk.

示例。使用字母表中的二十六个字母可以形成多少个三个字母的字符串(例如“xyz”、“qqa”……)?

Example. How many three-letter strings (like “xyz,” “qqa,” …) can be formed using the twenty-six letters of the alphabet?

解决方案。我们必须从 26 个元素中做出 3 个选择,总共263=17,576可能性。

Solution. We have to make three selections from a set of 26 elements, for a total of 263=17,576 possibilities.


假设我们想让k有序选择,但我们不允许重复。例如,如果一家博物馆的储藏室里有 26 幅画,必须从中挑选三幅画,沿着墙从左到右排列。第一个位置有 26 个选择。一旦我们做出了选择,第二个位置剩下 25 个,第三个位置剩下 24 个。因此,很明显,有262524总体安排。

Suppose instead we wish to make k ordered selections, but we are not allowed to repeat ourselves. This would arise, from example, if a museum had 26 paintings in its storeroom, and has to select three of them to put on display, ordered from left to right along a wall. There are 26 choices for the first position. Once we have made that choice, 25 remain for the second position, and then 24 remain for the third. So it seems clear that there are 262524 arrangements overall.

让我们试着用数学术语来阐述这个问题。我们可以想像一下有序选择k集合中的元素S无需重复即可作为单射函数 f[k]S. 元素f(0)是首选;f(1)是第二个选择,它必须不同于f(0)f(2)是第三种选择,它必须不同于f(0)f(1); 等等。

Let us try to frame the problem in mathematical terms. We can think of an ordered selection of k elements from a set S without repetition as being an injective function f from [k] to S. The element f(0) is the first choice; f(1) is the second choice, which has to be distinct from f(0); f(2) is the third choice, which has to be distinct from f(0) and f(1); and so on.


定理。AB是有限集,其中|A|=k|B|=n, 和kn. 来自的注入函数的数量ABn(n1)(nk+1)

Theorem. Let A and B be finite sets, with |A|=k and |B|=n, and kn. The number of injective functions from A to B is n(n1)(nk+1).

证明。利用归纳法k,我们将证明对于每一个AB, 和nk,断言成立。当k=0只有一个可注入函数,即具有空定义域的函数。假设A具有基数k+1, 让a0是任意元素A. 然后任何来自的注入函数AB可以通过选择一个元素来获得b0对于图像a0,然后从中选择一个注入函数A{a0}B{b0}。 有n选择b0,并且|A{a0}|=n1|B{b0}|=k1, 有(n1)(nk+1)根据归纳假设,注入函数的选择。

Proof. Using induction on k, we will show that for every A, B, and nk, the claim holds. When k=0 there is only one injective function, namely the function with empty domain. Suppose A has cardinality k+1, let a0 be any element of A. Then any injective function from A to B can be obtained by choosing an element b0 for the image of a0, and then choosing an injective function from A{a0} to B{b0}. There are n choices of b0, and since |A{a0}|=n1 and |B{b0}|=k1, there are (n1)(nk+1) choices of the injective function, by the inductive hypothesis.

定理。S是一个有限集,其中|S|=n. 然后制作方法的数量k选自S不允许重复的是n(n1)(nk+1)

Theorem. Let S be a finite set, with |S|=n. Then the number of ways of making k selections from S without repetition allowed is n(n1)(nk+1).

证明。这只是对前一个定理的重述,其中A=[k]B=S

Proof. This is just a restatement of the previous theorem, where A=[k] and B=S.


如果A是一个有限集,一个双射fAA也被称为A. 前面的定理表明,如果|A|=n然后排列数An(n1)1。这个数量出现的频率很高,所以它有一个名字,n 阶乘,以及一个特殊符号,n!.如果我们考虑元素A按某种顺序列出,A本质上是一个有序的选择n来自的元素A无需重复:我们选择将第一个元素映射到哪里,然后是第二个元素,依此类推。这是一个有用的惯例0!等于1

If A is a finite set, a bijection f from A to A is also called a permutation of A. The previous theorem shows that if |A|=n then the number of permutations of A is n(n1)1. This quantity comes up so often that it has a name, n factorial, and a special notation, n!. If we think of the elements of A listed in some order, a permutation of A is essentially an ordered selection of n elements from A without repetition: we choose where to map the first element, then the second element, and so on. It is a useful convention to take 0! to be equal to 1.

更一般的情况是,我们只选择k集合中的元素A被称为k-排列A. 上述定理表明k-排列n-元素集等于n!/(nk)!,因为如果你将分子和分母展开为乘积并取消,你就会得到n(n1)(nk+1).这个数字通常表示为P(n,k)或者Pkn或类似的变体。所以我们有P(n,k)=n!/(nk)!。请注意,等式右边的表达式提供了一种高效的方式来写出P(n,k),但计算方法效率不高。

The more general case where we are choosing only k elements from a set A is called a k-permutation of A. The theorem above says that the number of k-permutations of an n-element set is equal to n!/(nk)!, because if you expand the numerator and denominator into products and cancel, you get exactly the n(n1)(nk+1). This number is often denoted P(n,k) or Pkn, or some similar variant. So we have P(n,k)=n!/(nk)!. Notice that the expression on the right side of the equality provides an efficient way of writing the value of P(n,k), but an inefficient way of calculating it.

20.4.组合和二项式系数

20.4. Combinations and Binomial Coefficients

在上一节中,我们计算了博物馆从其储藏室的 26 幅画作中挑选三幅画作,将它们排列在一面墙上的方式数量。根据上一节中的最后观察,我们可以将这个数字写成26!/23!

In the last section, we calculated the number of ways in which a museum could arrange three paintings along a wall, chosen from among 26 paintings in its storeroom. By the final observation in the previous section, we can write this number as 26!/23!.

假设现在我们想计算博物馆从其储藏室中挑选三幅画作进行展出的方式数量,我们不关心顺序。换句话说,如果ab, 和c是绘画,我们不想区分选择a然后b然后c并选择c然后b然后a。当我们沿着整面墙排列绘画时,考虑这两种不同的排列方式是有意义的,但如果我们只关心最后得到的元素集,那么选择它们的顺序就无关紧要了。

Suppose now we want to calculate the number of ways that a museum can choose three paintings from its storeroom to put on display, where we do not care about the order. In other words, if a, b, and c are paintings, we do not want to distinguish between choosing a then b then c and choosing c then b then a. When we were arranging paintings along all wall, it made sense to consider these two different arrangements, but if we only care about the set of elements we end up with at the end, the order that we choose them does not matter.

问题是,每组三幅画会被计算多次。事实上,每幅画会被计算六次:3!=6集合的排列{a,b,c}例如。因此,要计算结果的数量,我们只需除以 6。换句话说,我们想要的数字是26!3!23!

The problem is that each set of three paintings will be counted multiple times. In fact, each one will be counted six times: there are 3!=6 permutations of the set {a,b,c}, for example. So to count the number of outcomes we simply need to divide by 6. In other words, the number we want is 26!3!23!.

这些数字没什么特别的263. 同样的公式也适用于我们称之为无序选择的 k 一组元素 n 元素,或k-组合来自 n-元素集。我们的目标是再次用精确的数学术语来描述这种情况,此时我们将能够将公式表述为定理。

There is nothing special about the numbers 26 and 3. The same formula holds for what we will call unordered selections of k elements from a set of n elements, or k-combinations from an n-element set. Our goal is once again to describe the situation in precise mathematical terms, at which point we will be able to state the formula as a theorem.

事实上,用更数学的术语来描述这种情况是相当容易的。如果S是一组n元素,无序选择k来自的元素S只是S具有基数k

In fact, describing the situation in more mathematical terms is quite easy to do. If S is a set of n elements, an unordered selection of k elements from S is just a subset of S that has cardinality k.


定理。S是具有基数的任何集合n,并让kn. 然后子集的数量S基数kn!k!(nk)!

Theorem. Let S be any set with cardinality n, and let kn. Then the number of subsets of S of cardinality k is n!k!(nk)!.

证明。U是无序选择的集合k来自的元素S, 让V是排列的集合[k],并让W是有序选择的集合k来自的元素S之间存在双射U×VW如下。假设我们分配给每个k-元素子集{a0,,ak1}S以某种方式列出元素,如图所示。然后给定任何这样的集合和任何排列f[k],我们得到一个有序的有序选择(af(0),af(1),,af(k1))。任何有序选择都源自这样的一个子集和一个合适的排列,因此映射是全射的。而不同的集合或不同的排列会导致不同的有序选择,因此映射是单射的。

Proof. Let U be the set of unordered selections of k elements from S, let V be the set of permutations of [k], and let W be the set of ordered selections of k elements from S. There is a bijection between U×V and W, as follows. Suppose we assign to every k-element subset {a0,,ak1} of S some way of listing the elements, as shown. Then given any such set and any permutation f of [k], we get an ordered the ordered selection (af(0),af(1),,af(k1)). Any ordered selection arises from such a subset and a suitable permutation, so the mapping is surjective. And a different set or a different permutation results in a different ordered selection, so the mapping is injective.

根据计数原理,我们有

By the counting principles, we have

P(n,k)=|W|=|U×V|=|U||V|=|U|k!,

所以我们有|U|=P(n,k)/k!=n!k!(nk)!

so we have |U|=P(n,k)/k!=n!k!(nk)!.

例如。某人要去度假,想从衣柜里的十套衣服中挑选三套装进行李箱。他们有多少种选择?

Example. Someone is going on vacation and wants to choose three outfits from ten in their closet to pack in their suitcase. How many choices do they have?

解决方案。 10!3!7!=1098321=120

Solution. 10!3!7!=1098321=120.


无序选择的数量k大小集合中的元素n或者,同样地,k-组合来自n-元素集,通常表示为(nk)C(n,k)Ckn或类似符号。我们将使用第一种符号,因为它最常见。请注意(n0)=1对于每一个n;这是有道理的,因为任何n元素集基数0

The number of unordered selections of k elements from a set of size n, or, equivalently, the number of k-combinations from an n-element set, is typically denoted by (nk), C(n,k), Ckn, or something similar. We will use the first notation, because it is most common. Notice that (n0)=1 for every n; this makes sense, because there is exactly one subset of any n-element set of cardinality 0.

这是该函数的一个重要属性。

Here is one important property of this function.


定理。对于每一个nkn,我们有(nk)=(nnk)

Theorem. For every n and kn, we have (nk)=(nnk).

证明。这是一个简单的计算:

Proof. This is an easy calculation:

n!(nk)!(n(nk))!=n!(nk)!k!.

但从组合解释中也很容易看出:选择k服装来自n休假和选择是同一项任务nk出门时的服装。

But it is also easy to see from the combinatorial interpretation: choosing k outfits from n to take on vacation is the same task as choosing nk outfits to leave home.


这是另一个重要属性。

Here is another important property.


定理。对于每一个nk, 如果k+1n, 然后

Theorem. For every n and k, if k+1n, then

(n+1k+1)=(nk+1)+(nk).

证明。理解这个定理的一种方法是从组合解释的角度。假设你想选择k+1服装出n+1把一套衣服放在一边,比如蓝色的。然后你有两个选择:你可以选择k+1其余服装,(nk+1)可能性;或者你可以拿蓝色的,然后选择k从剩下的衣服中挑选合适的服装。

Proof. One way to understand this theorem is in terms of the combinatorial interpretation. Suppose you want to choose k+1 outfits out of n+1. Set aside one outfit, say, the blue one. Then you have two choices: you can either choose k+1 outfits from the remaining ones, with (nk+1) possibilities; or you can take the blue one, and choose k outfits from the remaining ones.

该定理也可以通过直接计算来证明。我们可以将等式的左边表示如下:

The theorem can also be proved by direct calculation. We can express the left-hand side of the equation as follows:

(n+1k+1)=(n+1)!(k+1)!((n+1)(k+1))!=(n+1)!(k+1)!(nk)!.

类似地,我们可以简化右边:

Similarly, we can simplify the right-hand side:

(nk+1)+(nk)=n!(k+1)!(n(k+1))!+n!k!(nk)!=n!(nk)(k+1)!(nk1)!(nk)+(k+1)n!(k+1)k!(nk)!=n!(nk)(k+1)!(nk)!+(k+1)n!(k+1)!(nk)!=n!(nk+k+1)(k+1)!(nk)!=n!(n+1)(k+1)!(nk)!=(n+1)!(k+1)!(nk)!.

因此左边和右边相等。

Thus the left-hand side and the right-hand side are equal.


对于每一个n,我们知道(n0)=(nn)=1。前面的定理给出了计算所有二项式系数的方法:一旦我们确定(nk)对于一些n以及每一个kn,我们可以确定(n+1k)对于每一个kn+1使用上面的方法。结果可以以图形方式显示在所谓的帕斯卡三角形中:

For every n, we know (n0)=(nn)=1. The previous theorem then gives a recipe to compute all the binomial coefficients: once we have determine (nk) for some n and every kn, we can determine the values of (n+1k) for every kn+1 using the recipe above. The results can be displayed graphically in what is known as Pascal’s triangle:

具体来说,如果我们从0, 这k第元素n第行等于(nk)

Specifically, if we start counting at 0, the kth element of the nth row is equal to (nk).

也存在着(nk)和多项式(a+b)n,即k系数(a+b)n正是(nk)。例如,我们有

There is also a connection between (nk) and the polynomials (a+b)n, namely, that the kth coefficient of (a+b)n is exactly (nk). For example, we have

(a+b)4=a4+4a3b+6a2b2+4ab3+b4.

因此,价值观(nk)通常被称为二项式系数,并且陈述

For that reason, the values (nk) are often called binomial coefficients, and the statement that

(a+b)n=kn(nk)ankbk

被称为二项式定理

is known as the binomial theorem.

有几种方法可以说明为什么这个定理成立。一种方法是展开多项式,

There are a couple of ways of seeing why this theorem holds. One is to expand the polynomial,

(a+b)n=(a+b)(a+b)(a+b)

并注意到该项的系数ankbk等于取加数的方法数b正是k职位,以及a在剩下的nk位置。证明结果的另一种方法是使用归纳法n并使用身份(n+1k+1)=(nk+1)+(nk)。细节留作练习。

and notice that the coefficient of the term ankbk is equal to the number of ways of taking the summand b in exactly k positions, and a in the remaining nk positions. Another way to prove the result is to use induction on n, and use the identity (n+1k+1)=(nk+1)+(nk). The details are left as an exercise.

最后,我们考虑了有重复和无重复的有序选择,以及无重复的无序选择。那么有重复的无序选择呢?换句话说,给定一个集合Sn元素,我们想知道有多少种方法可以制作k选择,我们可以选择元素S重复,但我们只关心每个元素被选择的次数,而不是顺序。我们有以下内容:

Finally, we have considered ordered selections with and without repetitions, and unordered selections without repetitions. What about unordered selections with repetitions? In other words, given a set S with n elements, we would like to know how many ways there are of making k choices, where we can choose elements of S repeatedly, but we only care about the number of times each element was chosen, and not the order. We have the following:


无序选择的数量k来自n元素集,重复,是(n+k1k)

The number of unordered selections of k elements from an n-element set, with repetition, is (n+k1k).


练习中概述了这一点的证明。

A proof of this is outlined in the exercises.

20.5.包含-排斥原理

20.5. The Inclusion-Exclusion Principle

AB是某个域的任意两个子集,U。 然后A=AB(AB),并且并集中的两个集合不相交,因此我们有|A|=|AB|+|AB|。 这意味着|AB|=|A||AB|直观上看,这是有道理的:我们可以计算AB通过计算元素A,然后减去同时在AB

Let A and B be any two subsets of some domain, U. Then A=AB(AB), and the two sets in the union are disjoint, so we have |A|=|AB|+|AB|. This means |AB|=|A||AB|. Intuitively, this makes sense: we can count the elements of AB by counting the elements in A, and then subtracting the number of elements that are in both A and B.

同样地,我们有AB=A(BA),而等式右边的两组是不相交的,所以我们有

Similarly, we have AB=A(BA), and the two sets on the right-hand side of this equation are disjoint, so we have

|AB|=|A|+|BA|=|A|+|B||AB|.

如果我们画一个维恩图,这是有道理的:计算AB,我们可以添加元素的数量A元素的数量B,但随后我们必须减去两者的元素数量。

If we draw a Venn diagram, this makes sense: to count the elements in AB, we can add the number of elements in A to the number of elements in B, but then we have to subtract the number of elements of both.

当有三个集合时会发生什么?计算|ABC|,我们可以先把每个元素的数量相加,然后减去|AB||AC|, 和|BC|,每个元素都被重复计算了。但思考一下维恩图应该可以帮助我们意识到,我们过度修正了:ABC在原始总数中被计算了三次,并且减去了三次。所以我们需要把它们加回去:

What happen when there are three sets? To compute |ABC|, we can start by adding the number of elements in each, and then subtracting the number of elements of |AB|, |AC|, and |BC|, each of which have been double-counted. But thinking about the Venn diagram should help us realize that then we have over-corrected: each element of ABC was counted three times in the original sum, and the subtracted three times. So we need to add them back in:

|ABC|=|A|+|B|+|C||AB||AC||BC|+|ABC|.

这可以推广到任意数量的集合。为了说明一般结果,假设集合被编号A0,,An1. 对于每个非空子集I{0,,n1}, 考虑iIAi。 如果|I|为奇数(即等于 1、3、5、…),我们要添加交集的基数;如果是偶数,我们要减去它。此配方可通过以下公式简洁地表达:

This generalizes to any number of sets. To state the general result, suppose the sets are numbered A0,,An1. For each nonempty subset I of {0,,n1}, consider iIAi. If |I| is odd (that is, equal to 1, 3, 5, …) we want to add the cardinality of the intersection; if it is even we want to subtract it. This recipe is expressed compactly by the following formula:

|i<nAi|=I[n](1)|I|+1|iIAi|.

如果你雄心勃勃,欢迎你尝试证明这一点。以下示例说明了它的用法:

You are invited to try proving this as an exercise, if you are ambitious. The following example illustrates its use:


示例。在一组大学新生中,有 30 人选修逻辑,25 人选修历史,20 人选修法语。此外,有 11 人选修逻辑和历史,10 人选修逻辑和法语,7 人选修历史和法语,3 人选修全部三门课程。有多少名学生选修了至少一门课程?

Example. Among a group of college Freshmen, 30 are taking Logic, 25 are taking History, and 20 are taking French. Moreover, 11 are taking Logic and History, 10 are taking Logic and French, 7 are taking History and French, and 3 are taking all three. How many students are taking at least one of the three classes?

解决方案。LH, 和F分别表示选修逻辑、历史和法语的学生集合,我们有

Solution. Letting L, H, and F denote the sets of students taking Logic, History, and French, respectively, we have

|LHF|=30+25+2011107+3=50.

20.6.练习

20.6. Exercises

  1. 假设在聚会上,每两个人要么互相认识,要么不认识。换句话说,“x知道y”是对称的。另外,让我们忽略我们是否总是通过将注意力限制在不同人之间的关系上来了解自己的复杂问题;换句话说,对于这个问题,取“x知道y” 也是不反身的。

    使用鸽巢原理(和额外的见解)来表明一定有两个人认识完全相同数量的人。

  2. Suppose that, at a party, every two people either know each other or don’t. In other words, “x knows y” is symmetric. Also, let us ignore the complex question of whether we always know ourselves by restricting attention to the relation between distinct people; in other words, for this problem, take “x knows y” to be irreflexive as well.

    Use the pigeonhole principle (and an additional insight) to show that there must be two people who know exactly the same number of people.

  3. 证明在任意一组n+1整数,其中两个是模等价的n

  4. Show that in any set of n+1 integers, two of them are equivalent modulo n.

  5. 详细阐述第 20.2 节中第二计数原理的证明。

  6. Spell out in detail a proof of the second counting principle in Section 20.2.

  7. 一家冰淇淋店有 31 种口味的冰淇淋。

    1. 如果我们关心顺序并且允许重复,确定可能有多少种三种口味的冰淇淋甜筒。(因此,从下到上选择巧克力-巧克力-香草冰淇淋球与选择巧克力-香草-巧克力冰淇淋球是不同的。)

    2. 如果我们关心顺序,但不允许重复,确定有多少种三种口味的冰淇淋蛋筒。

    3. 如果我们不关心顺序,但不允许重复,则确定有多少种三种口味的冰淇淋蛋筒。

  8. An ice cream parlor has 31 flavors of ice cream.

    1. Determine how many three-flavor ice-cream cones are possible, if we care about the order and repetitions are allowed. (So choosing chocolate-chocolate-vanilla scoops, from bottom to top, is different from choosing chocolate-vanilla-chocolate.)

    2. Determine how many three flavor ice-cream cones are possible, if we care about the order, but repetitions are not allowed.

    3. Determine how many three flavor ice-cream cones are possible, if we don’t care about the order, but repetitions are not allowed.

  9. 一个有 10 人的俱乐部需要选举一名主席、一名副主席和一名秘书。有多少种选择:

    1. 如果一个人不能担任多个职务?

    2. 是否任何人都可以担任任意数量的这些职务?

    3. 是否有人可以担任最多两个职务?

    4. 如果总统不能担任其他职务,但副总统和秘书可能是或不是同一个人?

  10. A club of 10 people has to elect a president, vice president, and secretary. How many possibilities are there:

    1. if no person can hold more than one office?

    2. if anyone can hold any number of those offices?

    3. if anyone can hold up to two offices?

    4. if the president cannot hold another office, but the vice president and secretary may or may not be the same person?

  11. 如果可以使用任意 7 位数字,那么 7 位电话号码有多少个?如果第一位数字不能为 0,那么有多少个?

  12. How many 7 digit phone numbers are there, if any 7 digits can be used? How many are there if the first digit cannot be 0?

  13. 幼儿园一个班有 20 名学生,其中有两名是双胞胎。有多少种排学生的方法,可以让双胞胎站在一起?

  14. In a class of 20 kindergarten students, two are twins. How many ways are there of lining up the students, so that the twins are standing together?

  15. 一位女士的书架上有 8 本谋杀悬疑小说,她想在度假时带上其中三本。她有多少种方法可以做到这一点?

  16. A woman has 8 murder mysteries sitting on her shelf, and wants to take three of them on a vacation. How many ways can she do this?

  17. 在扑克中,“满堂红”是指一手牌中有三张同等级的牌和两张其他等级的牌(例如三张 K 和两张 5)。确定一副普通的 52 张牌可以组成满堂红的数量。

  18. In poker, a “full house” is a hand with three of one rank and two of another (for example, three kings and two fives). Determine the number of full houses that can be formed from an ordinary deck of 52 cards.

  19. 我们在第 20.4 节中看到

    (n+1k+1)=(nk+1)+(nk).

    更换k+1经过k,无论何时1kn,我们有

    (n+1k)=(nk)+(nk1).

    以此证明,通过归纳n,对于每一个kn,如果S是任意的集合n元素,(nk)是子集的数量Sk元素。

  20. We saw in Section 20.4 that

    (n+1k+1)=(nk+1)+(nk).

    Replacing k+1 by k, whenever 1kn, we have

    (n+1k)=(nk)+(nk1).

    Use this to show, by induction on n, that for every kn, that if S is any set of n elements, (nk) is the number of subsets of S with k elements.

  21. MISSISSIPPI 这个词中字母有多少种不同的排列?

    (提示:这很棘手。首先,假设所有的 S、I 和 P 都被涂上了不同的颜色。然后确定字母有多少种不同的排列。在没有区分颜色的情况下,确定每种配置在第一次计数中出现的次数,然后除以该数字。)

  22. How many distinct arrangements are there of the letters in the word MISSISSIPPI?

    (Hint: this is tricky. First, suppose all the S’s, I’s, and P’s were painted different colors. Then determine how many distinct arrangements of the letters there would be. In the absence of distinguishing colors, determine how many times each configuration appeared in the first count, and divide by that number.)

  23. 证明容斥原理。

  24. Prove the inclusion-exclusion principle.

  25. 使用包含-排除原理确定可以被 2、3 或 5 整除的小于 100 的整数的数量。

  26. Use the inclusion-exclusion principle to determine the number of integers less than 100 that are divisible by 2, 3, or 5.

  27. 显示无序选择的数量k来自n-元素集是(n+k1k)

    提示:考虑[n]我们需要选择一些数字i0由 0 组成,某个数字i11,等等,这样i0+i1++in1=k假设我们为每个这样的元组分配以下二进制序列:我们写下i00,然后是 1,然后i10,然后是 1,然后i20,依此类推。结果是一个长度为n+k1正是k1,这样的二进制序列就这样从一个唯一的元组产生。

  28. Show that the number of unordered selections of k elements from an n-element set is (n+k1k).

    Hint: consider [n]. We need to choose some number i0 of 0’s, some number i1 of 1’s, and so on, so that i0+i1++in1=k. Suppose we assign to each such tuple a the following binary sequence: we write down i0 0’s, then a 1, then i1 0’s, then a 1, then i2 0’s, and so on. The result is a binary sequence of length n+k1 with exactly k 1’s, and such binary sequence arises from a unique tuple in this way.

21.实数

21. The Real Numbers

21.1.数字系统

21.1. The Number Systems

我们已经接触过一些基本的数字系统:自然数,N,整数,Z,以及有理数,Q。从某种意义上说,列表中的每个后续元素都是为了弥补先前系统的缺陷而设计的。我们可以用任何整数减去另一个整数,得到另一个整数,也可以用任何有理数除以非零有理数,得到一个有理数。

We have already come across some of the fundamental number systems: the natural numbers, N, the integers, Z, and the rationals, Q. In a sense, each subsequent element of the list was designed to remedy defects in the previous system. We can subtract any integer from any other integer and end up with another integer, and we can divide any rational number by a nonzero rational number and end up with a rational number.

整数满足以下所有属性:

The integers satisfy all of the following properties:

  • 加法具有结合律和交换律。

  • Addition is associative and commutative.

  • 有一个附加恒等式,0,并且每个元素x有一个加法逆元,x

  • There is an additive identity, 0, and every element x has an additive inverse, x.

  • 乘法是结合律和交换律。

  • Multiplication is associative and commutative.

  • 有一个乘法恒等式,1

  • There is a multiplicative identity, 1.

  • 乘法分配于加法:对于每个xy, 和z,我们有x(y+z)=xy+xz

  • Multiplication distributes over addition: for every x, y, and z, we have x(y+z)=xy+xz.

  • 排序是一个全序。

  • The ordering is a total order.

  • 对于任意元素xy, 和z, 如果xy然后x+zy+z

  • For any elements x, y, and z, if xy then x+zy+z.

  • 对于任意元素xy, 如果0x0y然后0xy

  • For any elements x and y, if 0x and 0y then 0xy.

前五条规定×+0, 和1,整数形成一个交换环,最后三个表示与,结构为有序环。自然数缺乏加法逆元,因此它们满足一组稍弱的公理,使它们成为有序半环。另一方面,有理数也形成有序环,满足以下附加属性:

The first five clauses say that with ×, +, 0, and 1, the integers form a commutative ring, and the last three say that together with , the structure is an ordered ring. The natural numbers lack additive inverses, so they satisfy a slightly weaker set of axioms that make them an ordered semiring. On the other hand, the rational numbers also form an ordered ring, satisfying the following additional property:

  • 每个非零元素都有一个乘法逆元,x1

  • Every nonzero element has a multiplicative inverse, x1.

这使得它们成为有序字段的实例。

This makes them an instance of an ordered field.

值得一提的是,一旦我们有了自然数,就可以使用你已经见过的集合论构造来构造整数和有理数。例如,我们可以将一个整数视为一对(i,n)自然数i为 0 或 1,其目的是(0,n)表示正整数n, 和(1,n)表示负整数(n+1).(我们使用(n+1)而不是n避免出现两种表示形式0)有了这个定义,整数就是{0,1}×N然后我们可以继续定义加法和乘法运算、加法逆元和序关系,并证明它们具有所需的性质。

It is worth knowing that once we have the natural numbers, it is possible to construct the integers and rational numbers, using set-theoretic constructions you have already seen. For example, we can take an integer to be a pair (i,n) of natural numbers where i is either 0 or 1, with the intention that (0,n) represents the positive integer n, and (1,n) represents the negative integer (n+1). (We use (n+1) instead of n to avoid having two representations of 0.) With this definition, the integers are simply {0,1}×N. We can then go on to define the operations of addition and multiplication, the additive inverse, and the order relation, and prove they have the desired properties.

这种构造的副作用是自然数本身不是整数;例如,我们必须区分自然数2和整数2。在 Lean 中就是这种情况。在普通数学中,通常将自然数视为整数的子集。然而,一旦我们构造了整数,我们就可以抛弃旧版本的自然数,然后将自然数标识为非负整数。

This construction has the side effect that the natural numbers themselves are not integers; for example, we have to distinguish between the natural number 2 and the integer 2. This is the case in Lean. In ordinary mathematics, it is common to think of the natural numbers as a subset of the integers. Once we construct the integers, however, we can throw away the old version of the natural numbers, and afterwards identify the natural numbers as nonnegative integers.

我们可以对有理数做同样的事情,将它们定义为对的集合(a,b)Z×N,其中a=0b=1, 或者b>0ab没有共同的除数(除了11)这个想法是(a,b)代表a/b有理数实际上是Z×N,然后我们可以相应地定义所有的操作。

We can do the same for the rationals, defining them to be the set of pairs (a,b) in Z×N, where either a=0 and b=1, or b>0 and a and b have no common divisor (other than 1 and 1). The idea is that (a,b) represents a/b. With this definition, the rationals are really a subset of Z×N, and we can then define all the operations accordingly.

在下一节中,我们将定义一种更复杂的方法,这种方法将扩展到实数的定义。在后面的章节中,我们将展示如何从集合论的公理中构造自然数。这表明我们可以从下往上构建所有的数字系统。

In the next section, we will define a more sophisticated approach, one which will scale to a definition of the real numbers. And in a later chapter, we will show how to construct the natural numbers from the axioms of set theory. This shows that we can construct all the number systems from the bottom up.

但首先,让我们暂停一下,考虑一下为什么需要实数。我们已经看到2没有有理数的平方根。这意味着,在某种意义上,有理数中存在一个“缺口”:有理数的平方任意接近于 2,但没有有理数x具有以下属性x2=2。但直观上看,应该有某个数字具有该属性:2是边长为1。 相似地,π,即半径为 1 的圆的面积,在有理数中是缺失的。实数就是用来弥补这类缺陷的。

But first, let us pause for a moment to consider why the real numbers are needed. We have seen that 2 has no rational square root. This means, in a sense, that there is a “gap” in the rationals: the are rationals whose squares are arbitrarily close to 2, but there is no rational x with the property that x2=2. But it seems intuitively clear that there should be some number with that property: 2 is the length of the diagonal of a square with side length 1. Similarly, π, the area of a circle with radius 1, is missing from the rationals. These are the kinds of defects that the real numbers are designed to repair.

你可能习惯将实数视为(潜在的)无限小数:例如,2=1.41421356π=3.14159265本章的一个核心目标是使“……”精确化。这个想法是,我们可以用无限小数来表示有理近似序列。例如,我们可以用序列来近似 2 的平方根1,1.4,1.41,1.414,.我们想定义2作为该数列的“极限”,但我们已经看到,该数列在有理数中没有极限。因此,我们必须构造新的对象,即实数,来实现这一目的。

You may be used to thinking of real numbers as (potentially) infinite decimals: for example, 2=1.41421356 and π=3.14159265. A central goal of this chapter is to make the “…” precise. The idea is that we can take an infinite decimal to represent a sequence of rational approximations. For example, we can approximate the square root of 2 with the sequence 1,1.4,1.41,1.414,. We would like to define 2 to be the “limit” of that sequence, but we have seen that the sequence does not have a limit in the rationals. So we have to construct new objects, the real numbers, to serve that purpose.

事实上,我们将实数或多或少地定义为有理近似的序列。但我们必须面对这样一个事实,例如,有很多方法可以近似 2 的平方根。例如,我们也可以从上面来处理它,2,1.5,1.42,,或者上下振荡。下一节将向我们展示如何将所有这些序列“粘合”在一起并将它们视为单个对象。

In fact, we will define the real numbers, more or less, to be such sequences of rational approximations. But we will have to deal with the fact that, for example, there are lots of ways of approximating the square root of two. For example, we can just as well approach it from above, 2,1.5,1.42,, or by oscillating above and below. The next section will show us how to “glue” all these sequences together and treat them as a single object.

21.2.商构造

21.2. Quotient Constructions

A是任意集合,并设是任意的等价关系A回想一下13.3 节,我们可以为每个元素分配aA等价类[a], 在哪里b[a]方法ba。此分配具有以下属性:对于每个abab当且仅当[a]=[b]

Let A be any set, and let be any equivalence relation on A. Recall from Section 13.3 that we can assign to every element a of A the equivalence class [a], where b[a] means ba. This assignment has the property that for every a and b, ab if and only if [a]=[b].

给定任意集合A和等价关系, 定义A/成为集合{[a]aA}等价类​A模数。这套作品名为“A模数,或A经过。你可以将其视为集合A其中等效元素“粘合在一起”以形成更粗糙的集合。

Given any set A and equivalence relation , define A/ to be the set {[a]aA} of equivalence classes of A modulo . This set is called “A modulo ,” or the quotient of A by . You can think of this as the set A where equivalent elements are “glued together” to make a coarser set.

例如,如果我们考虑整数Z表示模 5 等价(如第 19.4 节所述),则Z/只是{[0],[1],[2],[3],[4]}我们可以定义加法Z/经过[a]+[b]=[a+b]。为了使这个定义有意义,重要的是要知道右边不依赖于哪些代表[a][b]我们选择。换句话说,我们需要知道,无论何时[a]=[a][b]=[b], 然后[a+b]=[a+b]。这反过来又相当于说,如果aabb, 然后a+ba+b换句话说,我们要求加法运算遵循等价关系,我们在第 19.4 节中看到,事实上确实如此。

For example, if we consider the integers Z with denoting equivalence modulo 5 (as in Section 19.4), then Z/ is just {[0],[1],[2],[3],[4]}. We can define addition on Z/ by [a]+[b]=[a+b]. For this definition to make sense, it is important to know that the right-hand side does not depend on which representatives of [a] and [b] we choose. In other words, we need to know that whenever [a]=[a] and [b]=[b], then [a+b]=[a+b]. This, in turn, is equivalent to saying that if aa and bb, then a+ba+b. In other words, we require that the operation of addition respects the equivalence relation, and we saw in Section 19.4 that this is in fact the case.

将定义在集合上的函数转换为定义在该集合的商上的函数的一般策略由以下定理给出。

This general strategy for transferring a function defined on a set to a function defined on a quotient of that set is given by the following theorem.


定理。AB是任意集合,设是定义在A,并让f:AB。 认为f尊重等价关系,也就是说,对于每一个aaA, 如果aa, 然后f(a)=f(a).然后有一个独特的功能f¯:A/B,定义为f¯([a])=f(a)对于每一个aA

Theorem. Let A and B be any sets, let be any equivalence relation defined on A, and let f:AB. Suppose f respects the equivalence relation, which is to say, for every a and a in A, if aa, then f(a)=f(a). Then there is a unique function f¯:A/B, defined by f¯([a])=f(a) for every a in A.

证明。我们已经定义了f¯在等价类上x通过写作x=[a]并设置f¯(x)=f(a)换句话说,我们说f¯(x)=y当且仅当存在a使得x=[a], 和f(a)=y. 这个定义令人怀疑的地方在于,它可能取决于我们如何表达x以这种形式;换句话说,我们需要证明存在一个独特的 y符合这一描述。具体来说,我们需要知道如果x=[a]=[a], 然后f(a)=f(a).但自从[a]=[a]相当于aa,这就等于说f尊重等价关系,这正是我们所假设的。

Proof. We have defined the value of f¯ on an equivalence class x by writing x=[a], and setting f¯(x)=f(a). In other words, we say that f¯(x)=y if and only if there is an a such that x=[a], and f(a)=y. What is dubious about the definition is that, a priori, it might depend on how we express x in that form; in other words, we need to show that there is a unique y meeting this description. Specifically, we need to know that if x=[a]=[a], then f(a)=f(a). But since [a]=[a] is equivalent to aa, this amounts to saying that f respects the equivalence relation, which is exactly what we have assumed.


数学家经常“定义”f¯根据方程f¯([a])=f(a),然后将上述证明表达为“f¯是定义明确的。”这很令人困惑。他们真正想表达的是定理所说的,即存在一个唯一函数满足该描述。

Mathematicians often “define” f¯ by the equation f¯([a])=f(a), and then express the proof above as a proof that “f¯ is well defined.” This is confusing. What they really mean is what the theorem says, namely, that there is a unique function meeting that description.

要构造整数,首先N×N. 想想自然数对(m,n)作为代表mn,其中减法发生在整数中(我们还没有构造!)。例如,(2,5)(6,9)表示整数3直观地看,对(m,n)(m,n)将表示相同的整数,当mn=mn,但我们还不能这么说,因为我们还没有定义适当的减法概念。但这个等式等价于m+n=m+n对于自然数的加法来说是有意义的。

To construct the integers, start with N×N. Think of the pair of natural numbers (m,n) as representing mn, where the subtraction takes place in the integers (which we haven’t constructed yet!). For example, both (2,5) and (6,9) represent the integer 3. Intuitively, the pairs (m,n) and (m,n) will represent the same integer when mn=mn, but we cannot say this yet, because we have not yet defined the appropriate notion of subtraction. But the equation is equivalent to m+n=m+n, and this makes sense with addition on the natural numbers.


定义。定义关系N×N经过(m,n)(m,n)当且仅当m+n=m+n

Definition. Define the relation on N×N by (m,n)(m,n) if and only if m+n=m+n.

主张。 是等价关系。

Proposition. is an equivalence relation.

证明。对于反身性,显然(m,n)(m,n), 自从m+n=m+n

Proof. For reflexivity, it is clear that (m,n)(m,n), since m+n=m+n.

为了对称,假设(m,n)(m,n)。 这意味着m+n=m+n. 但平等的对称性意味着(m,n)(m,n),按要求。

For symmetry, suppose (m,n)(m,n). This means m+n=m+n. But the symmetry of equality implies (m,n)(m,n), as required.

对于传递性,假设(m,n)(m,n), 和(m,n)=(m,n).然后我们有m+n=m+nm+n=n+m. 将这些方程相加,我们得到

For transitivity, suppose (m,n)(m,n), and (m,n)=(m,n). Then we have m+n=m+n and m+n=n+m. Adding these equations, we get

m+n+m+n=m+n+n+m.

减法m+n从两边我们得到m+n=n+m,相当于(m,n)=(m,n),按要求。

Subtracting m+n from both sides, we get m+n=n+m, which is equivalent to (m,n)=(m,n), as required.


现在我们可以定义整数为N×N/我们应该如何定义加法?如果[(m,n)]代表mn, 和[(u,v)]代表uv, 然后[(m,n)]+[(u,v)]应该代表(m+u)(n+v)。因此,定义[(m,n)]+[(u,v)][(m+u)(n+v)]。为了实现这一点,我们需要知道发送(m,n)(u,v)(m+u,n+v)尊重等价关系。

We can now define the integers to be N×N/. How should we define addition? If [(m,n)] represents mn, and [(u,v)] represents uv, then [(m,n)]+[(u,v)] should represent (m+u)(n+v). Thus, it makes sense to define [(m,n)]+[(u,v)] to be [(m+u)(n+v)]. For this to work, we need to know that the operation which sends (m,n) and (u,v) to (m+u,n+v) respects the equivalence relation.


命题。如果(m,n)(m,n)(u,v)(u,v), 然后(m+u,n+v)(m+u,n+v)

Proposition. If (m,n)(m,n) and (u,v)(u,v), then (m+u,n+v)(m+u,n+v).

证明。第一个等价意味着m+n=m+n,第二个意思是u+v=u+v. 将两个方程相加,我们得到(m+u)+(n+v)(m+u)+(n+v),这完全等同于说(m+u,n+v)(m+u,n+v)

Proof. The first equivalence means m+n=m+n, and the second means u+v=u+v. Adding the two equations, we get (m+u)+(n+v)(m+u)+(n+v), which is exactly the same as saying (m+u,n+v)(m+u,n+v).


每个自然数n可以用整数表示[(n,0)],特别是,0表示为[(0,0)].此外,如果[(m,n)]是任意整数,我们可以将其否定定义为[(n,m)], 自从[(m,n)]+[(n,m)]=[(m+n,n+m)]=[(0,0)], 自从(m+n,n+m)(0,0)简而言之,我们“发明”了负数!

Every natural number n can be represented by the integer [(n,0)], and, in particular, 0 is represented by [(0,0)]. Moreover, if [(m,n)] is any integer, we can define its negation to be [(n,m)], since [(m,n)]+[(n,m)]=[(m+n,n+m)]=[(0,0)], since (m+n,n+m)(0,0). In short, we have “invented” the negative numbers!

我们可以继续以这种方式定义整数的乘法和排序,并证明它们具有所需的属性。我们也可以对有理数进行类似的构造。在这里,我们将从集合开始Z×Z>0, 在哪里Z>0表示严格正整数。当然,这个想法是(a,b)代表(a/b)考虑到这一点,定义(a,b)(c,d)如果ad=bc我们可以继续定义加法、乘法以及其中的排序。然而,细节很繁琐,而且不太有启发性。所以我们转而讨论实数的构造。

We could go on this way to define multiplication and the ordering on the integers, and prove that they have the desired properties. We could also carry out a similar construction for the rational numbers. Here, we would start with the set Z×Z>0, where Z>0 denotes the strictly positive integers. The idea, of course, is that (a,b) represents (a/b). With that in mind, it makes sense to define (a,b)(c,d) if ad=bc. We could go on to define addition, multiplication, and the ordering there, too. The details are tedious, however, and not very illuminating. So we turn, instead, to a construction of the real numbers.

21.3.构造实数

21.3. Constructing the Real Numbers

我们面临的问题是1,1.4,1.41,1.414,1.4142,有理数的序列似乎趋近2 的平方根,但没有一个有理数能起到这个作用。下一个定义抓住了这样一个概念:这个数字序列“似乎趋近于一个值”,但并不是指它正在趋近的值。

The problem we face is that the sequence 1,1.4,1.41,1.414,1.4142, of rational numbers seems to approach a value that would be the square root of 2, but there is no rational number that can play that role. The next definition captures the notion that this sequence of numbers “seems to approach a value,” without referring to a value that it is approaching.


定义。有理数序列(qi)iN如果对于每个有理数,则为柯西ε>0,存在某个自然数NN对于所有i,jN,我们有|qiqj|<ε

Definition. A sequence of rational numbers (qi)iN is Cauchy if for every rational number ε>0, there is some natural number NN such that for all i,jN, we have that |qiqj|<ε.


粗略地说,柯西序列是元素变得任意接近的序列,不仅接近其后继元素,而且接近所有后续元素。在数学中,通常使用ε表示一个用来表示小东西的数量;你应该读一下这个短语“对于每一个ε>0” 就像说“无论多小ε是。”所以如果对于任意的ε>0,无论多小,总有一点N,超过该距离,元素将保持在ε彼此。

Roughly speaking, a Cauchy sequence is one where the elements become arbitrarily close, not just to their successors but to all following elements. It is common in mathematics to use ε to represent a quantity that is intended to denote something small; you should read the phrase “for every ε>0” as saying “no matter how small ε is.” So a sequence is Cauchy if, for any ε>0, no matter how small, there is some point N, beyond which the elements stay within a distance of ε of one another.

柯西序列可用于描述这些有理数之间的差距,但如上所述,许多柯西序列可用于描述相同的差距。在这个阶段,说它们“接近同一点”有点误导,因为它们没有接近的有理点;更准确的说法是,这些序列最终会变得任意接近。

Cauchy sequences can be used to describe these gaps in the rationals, but, as noted above, many Cauchy sequences can be used to describe the same gap. At this stage, it is slightly misleading to say that they “approach the same point,” since there is no rational point that they approach; a more precise statement is that the sequences eventually become arbitrarily close.


定义。两个柯西序列p=(pi)iNq=(qi)iN若对于每个有理数都是等价的ε>0,存在某个自然数NN对于所有iN,我们有|piqi|<ε.我们将写pq表达p相当于q

Definition. Two Cauchy sequences p=(pi)iN and q=(qi)iN are equivalent if for every rational number ε>0, there is some natural number NN such that for all iN, we have that |piqi|<ε. We will write pq to express that p is equivalent to q.

主张。 是柯西序列的等价关系。

Proposition. is an equivalence relation on Cauchy sequences.

证明。自反性和对称性很容易,所以让我们证明传递性。假设(pi)(qi)(qi)(ri).我们想证明序列(pi)相当于(ri).因此,给定任意ε>0, 选择N0足够大,使得对于每一个iN0|piqi|<ε/2. 选择其他号码,N1,因此对于每一个iN1|qiri|<ε/2。 让N=max(N0,N1).然后对于每一个iN,我们有

Proof. Reflexivity and symmetry are easy, so let us prove transitivity. Suppose (pi)(qi) and (qi)(ri). We want to show that the sequence (pi) is equivalent to (ri). So, given any ε>0, choose N0 large enough such that for every iN0, |piqi|<ε/2. Choose another number, N1, so that for every iN1, |qiri|<ε/2. Let N=max(N0,N1). Then for every iN, we have

|piri|=|(piqi)+(qiri)||piqi|+|qiri|<ε/2+ε/2=ε,

按要求。

as required.


请注意,证明中使用了三角不等式,该不等式指出,对于任何有理数ab|a+b||a|+|b|.如果我们定义|a|最大为aa,三角不等式实际上对任何有序环都成立:

Notice that the proof uses the triangle inequality, which states for any rational numbers a and b, |a+b||a|+|b|. If we define |a| to be the maximum of a and a, the triangle inequality in fact holds for any ordered ring:


定理。ab是任意有序环的元素。则|a+b||a|+|b|

Theorem. Let a and b be elements of any ordered ring. Then |a+b||a|+|b|.

证明。根据绝对值的定义,足以证明a+b|a|+|b|(a+b)|a|+|b|. 第一个主张源于以下事实:a|a|b|b|对于第二个断言,我们同样有a|a|b|b|, 所以(a+b)=a+b|a|+|b|

Proof. By the definition of absolute value, it suffices to show that a+b|a|+|b| and (a+b)|a|+|b|. The first claim follows from the fact that a|a| and b|b|. For the second claim, we similarly have a|a| and b|b|, so (a+b)=a+b|a|+|b|.


在上面的定理中,如果我们让a=xyb=yz,我们得到|xz||xy|+|yz|. 事实是|xy|表示之间的距离xy在数轴上解释了这个名字:对于任何三个“点”xy, 和z,距离xz不能大于xy加上距离yz

In the theorem above, if we let a=xy and b=yz, we get |xz||xy|+|yz|. The fact that |xy| represents the distance between x and y on the number line explains the name: for any three “points” x, y, and z, the distance from x to z can’t be any greater than the distance from x to y plus the distance from y to z.

我们现在让A是有理数柯西序列的集合,并定义实数,R,即A/换句话说,实数是有理数的柯西序列的集合,以我们刚刚定义的等价关系为模。

We now let A be the set of Cauchy sequences of rationals, and define the real numbers, R, to be A/. In other words, the real numbers are the set of Cauchy sequence of rationals, modulo the equivalence relation we just defined.

拥有一套R仅靠这一点是不够的:我们还想知道如何对实数进行加、减、乘、除。与整数一样,我们需要定义底层集合上的运算,然后证明它们遵循等价关系。例如,我们将说明如何对有理数的柯西序列进行加法,然后证明如果p1p2q1q2, 然后p1+q1p2+q2.然后我们可以将此定义提升为R通过定义[p]+[q][p+q]

Having the set R by itself is not enough: we also would like to know how to add, subtract, multiply, and divide real numbers. As with the integers, we need to define operations on the underlying set, and then show that they respect the equivalence relation. For example, we will say how to add Cauchy sequences of rationals, and then show that if p1p2 and q1q2, then p1+q1p2+q2. We can then lift this definition to R by defining [p]+[q] to be [p+q].

幸运的是,在柯西序列上定义加法、减法和乘法很容易。如果p=(pi)iNq=(qi)iN是柯西序列,设p+q=(pi+qi)iN,减法和乘法也类似。证明这些序列本身是柯西序列,并证明这些运算具有适当的代数性质则比较棘手。我们要求您在练习中证明其中一些性质。

Luckily, it is easy to define addition, subtraction, and multiplication on Cauchy sequences. If p=(pi)iN and q=(qi)iN are Cauchy sequences, let p+q=(pi+qi)iN, and similarly for subtraction and multiplication. It is trickier to show that these sequences are Cauchy themselves, and to show that the operations have the appropriate algebraic properties. We ask you to prove some of these properties in the exercises.

我们可以识别每一个有理数q常数柯西序列q,q,q,,因此实数包括所有有理数。下一步是抽象出我们选择的特定构造的细节,以便从今以后我们可以抽象地处理实数,而不再将它们视为由有理数的柯西序列的等价类给出。

We can identify each rational number q with the constant Cauchy sequence q,q,q,, so the real numbers include all the rationals. The next step is to abstract away the details of the particular construction we have chosen, so that henceforth we can work with the real numbers abstractly, and no longer think of them as given by equivalence classes of Cauchy sequences of rationals.

21.4.实数的完备性

21.4. The Completeness of the Real Numbers

我们构造了实数来填补有理数的空白。我们怎么知道我们已经得到了所有的实数?也许我们需要使用实数的柯西序列构造更多的数字?下一个定理告诉我们,相反,没有必要以这种方式进一步扩展实数。

We constructed the real numbers to fill in the gaps in the rationals. How do we know that we have got them all? Perhaps we need to construct even more numbers, using Cauchy sequences of reals? The next theorem tells us that, on the contrary, there is no need to extend the reals any further in this way.


定义。r是一个实数。序列(ri)iN实数收敛r如果,对于每一个ε>0,有一个N对于每一个iN|rir|<ε

Definition. Let r be a real number. A sequence (ri)iN of real numbers converges to r if, for every ε>0, there is an N such that for every iN, |rir|<ε.

定义。一个序列(ri)iN 如果它收敛到某个r

Definition. A sequence (ri)iN converges if it converges to some r.

定理。每个柯西实数序列都收敛。

Theorem. Every Cauchy sequence of real numbers converges.


该定理的表述通常表示为实数是完备的。粗略地说,它表示无论你在哪里寻找实数,你都一定会找到一个实数。这里有一个类似的原理。

The statement of the theorem is often expressed by saying that the real numbers are complete. Roughly, it says that everywhere you look for a real number, you are bound to find one. Here is a similar principle.


定义。一个元素uR被称为子集的上限SR如果一切S小于或等于uS如果有上限,则称其为有界S. 一个元素u被称为S如果它是S,并且不小于u是上限S

Definition. An element uR is said to be an upper bound to a subset SR if everything in S is less than or equal to u. S is said to be bounded if there is an upper bound to S. An element u is said to be a least upper bound to S if it is an upper bound to S, and nothing smaller than u is an upper bound to S.

定理。SR。 然后S有一个最小上界。

Theorem. Let S be a bounded, nonempty subset of R. Then S has a least upper bound.


有理数不具备这种性质:如果我们设定S={xQx2<2},那么有理数 2 就是S, 但S没有最小上界Q

The rational numbers do not have this property: if we set S={xQx2<2}, then the rational number 2 is an upper bound for S, but S has no least upper bound in Q.

一个基本定理是,实数的特征在于它们是完全有序域,因此每个实数r小于或等于某个自然数N满足这些要求的任何两个模型必须表现得完全相同,至少在常数方面01,操作+,以及关系关注。这一事实非常有力,因为它使我们避免在普通数学中思考柯西序列的构造。一旦我们证明我们的构造满足这些要求,我们就可以取R成为“唯一”的完全有序字段,忽略任何实现细节。我们也可以自由地实现R以任何我们选择的方式,只要它满足这个接口,并且只要它们不引用底层表示,我们证明的关于实数的任何定理都将同样适用于所有构造。

It is a fundamental theorem that the real numbers are characterized exactly by the property that they are a complete ordered field, such that every real number r is less than or equal to some natural number N. Any two models that meet these requirements must behave in exactly the same way, at least insofar as the constants 0 and 1, the operations + and , and the relation are concerned. This fact is extremely powerful because it allows us to avoid thinking about the Cauchy sequence construction in normal mathematics. Once we have shown that our construction meets these requirements, we can take R to be “the” unique complete totally ordered field and ignore any implementation details. We are also free to implement R in any way we choose, and as long as it meets this interface, and as long as they do not refer to the underlying representations, any theorems we prove about the reals will hold equally well for all constructions.

21.5.另一种构造方法

21.5. An Alternative Construction

许多资料使用实数的另一种构造方法,将它们视为Dedekind 截断。Dedekind 截断是有序对(A,B)具有以下属性的有理数集:

Many sources use an alternative construction of the reals, taking them instead to be Dedekind cuts. A Dedekind cut is an ordered pair (A,B) of sets of rational numbers with the following properties:

  • 每一个有理数q要么A或者B

  • Every rational number q is in either A or B.

  • 每个aA小于每一个bB

  • Each aA is less than every bB.

  • 没有最大的元素A

  • There is no greatest element of A.

  • AB均非空。

  • A and B are both nonempty.

前两个属性说明了为什么我们称这对为“切割”。集合A包含数轴上某个标记左边的所有有理数,并且B所有右边的点。第三个属性告诉我们在那个标记处到底发生了什么。但有两种可能:B有最小元素,或者没有。想象一下A没有最大元素,B没有最小元素可能有点棘手,但请考虑这个例子A={xQx2<2}B={xQx2>2}. 没有有理数q使得q2=2,但两边都有任意接近的有理数;因此A也不B包含一个端点。

The first two properties show why we call this pair a “cut.” The set A contains all of the rational numbers to the left of some mark on the number line, and B all of the points to the right. The third property tells us something about what happens exactly at that mark. But there are two possibilities: either B has a least element, or it doesn’t. Picturing the situation where A has no greatest element and B has no least element may be tricky, but consider the example A={xQx2<2} and B={xQx2>2}. There is no rational number q such that q2=2, but there are rational numbers on either side that are arbitrarily close; thus neither A nor B contains an endpoint.

我们可以定义R是 Dedekind 截断的集合。Dedekind 截断(A,B)对应于有理数q如果qB,并且如果B没有最小元素。R

We can define R to be the set of Dedekind cuts. A Dedekind cut (A,B) corresponds to a rational number q if q is the least element of B, and to an irrational number if B has no least element. It is straightforward to define addition on R:

(A1,B1)+(A2,B2)=({a1+a2a1A1,a2A2},{b1+b2b1B1,b2B2}).

有些作者更喜欢这种构造而不是柯西序列构造,因为它避免了取集合的商,从而消除了证明算术运算尊重等价性的复杂性。其他人更喜欢柯西序列,因为它们提供了更清晰的近似概念:如果一个实数r由柯西序列给出(qi)iN,然后任意接近有理近似r给出的是qN对于足够大的N

Some authors prefer this construction to the Cauchy sequence construction because it avoids taking the quotient of a set, and thus removes the complication of showing that arithmetic operations respect equivalence. Others prefer Cauchy sequences since they provide a clearer notion of approximation: if a real number r is given by a Cauchy sequence (qi)iN, then an arbitrarily close rational approximation of r is given by qN for a sufficiently large N.

不过,对于大多数数学家来说,大多数时候,这种差异并不重要。这两种构造都创建了完整的线性有序域,在某种意义上,它们创建的是相同的完整的线性有序域。严格地说,柯西实数集不等于戴德金实数集,因为一个由有理柯西序列的等价类组成,另一个由有理数集对组成。但这两个集合之间存在一个双射,可以保留域属性。也就是说,存在一个双射f从柯西实数到戴德金实数,使得

For most mathematicians most of the time, though, the difference is immaterial. Both constructions create complete linear ordered fields, and in a certain sense, they create the same complete linear ordered field. Strictly speaking, the set of Cauchy reals is not equal to the set of Dedekind reals, since one consists of equivalence classes of rational Cauchy sequences and one consists of pairs of sets of rationals. But there is a bijection between the two sets that preserves the field properties. That is, there is a bijection f from the Cauchy reals to the Dedekind reals such that

  • f(0)=0

  • f(0)=0

  • f(1)=1

  • f(1)=1

  • f(x+y)=f(x)+f(y)

  • f(x+y)=f(x)+f(y)

  • f(xy)=f(x)f(y)

  • f(xy)=f(x)f(y)

  • f(x)=f(x)

  • f(x)=f(x)

  • f(x1)=f(x)1

  • f(x1)=f(x)1

  • f(x)f(y)xy

  • f(x)f(y)xy

我们称这两个构造是同构的,并且函数f同构。由于我们通常只关心实数作为完全有序域的状态,并且这两个构造作为有序域是无法区分的,因此使用哪种构造没有区别。

We say that the two constructions are isomorphic, and that the function f is an isomorphism. Since we often only care about the real numbers in regard to their status as a complete ordered field, and the two constructions are indistinguishable as ordered fields, it makes no difference which construction is used.

21.6.练习

21.6. Exercises

  1. 证明第 21.2 节中定义的整数加法是交换律和结合律。

  2. Show that addition for the integers, as defined in Section 21.2, is commutative and associative.

  3. 根据第 21.2 节中的整数构造证明a+0=a对于每个整数a

  4. Show from the construction of the integers in Section 21.2 that a+0=a for every integer a.

  5. 定义整数减法ab=a+(b)并证明ab+b=a对于每一对整数ab

  6. Define subtraction for the integers by ab=a+(b), and show that ab+b=a for every pair of integers a and b.

  7. 定义整数的乘法,首先在底层表示上定义它,然后表明该运算遵循等价关系。

  8. Define multiplication for the integers, by first defining it on the underlying representation and then showing that the operation respects the equivalence relation.

  9. 证明每个柯西序列都是有界的:即若(qi)iN是柯西,有理M使得|qi|M面向所有人i. 提示:尝试让ε=1

  10. Show that every Cauchy sequence is bounded: that is, if (qi)iN is Cauchy, there is some rational M such that |qi|M for all i. Hint: try letting ε=1.

  11. p=(pi)iNq=(qi)iN是柯西序列。定义p+q=(pi+qi)iNpq=(piqi)iN

    1. 表明p+q是柯西的。也就是说,对于任意ε>0,表明存在一个N对于所有i,jN|(pi+qi)(pj+qj)|<ε

    2. 表明pq是柯西。除了三角不等式之外,你会发现前面的练习很有用。

  12. Let p=(pi)iN and q=(qi)iN be Cauchy sequences. Define p+q=(pi+qi)iN and pq=(piqi)iN.

    1. Show that p+q is Cauchy. That is, for arbitrary ε>0, show that there exists an N such that for all i,jN, |(pi+qi)(pj+qj)|<ε.

    2. Show that pq is Cauchy. In addition to the triangle inequality, you will find the previous exercise useful.

  13. 这两部分证明了柯西序列的加法满足等价性。

    1. 表明如果p,p,q是柯西序列,并且pp, 然后p+qp+q

    2. 使用该问题的第一部分,证明如果p,p,q,q是柯西序列,pp, 和qq, 然后p+qp+q。你可以利用实数加法可交换的事实。

  14. These two parts show that addition of Cauchy sequences respects equivalence.

    1. Show that if p,p,q are Cauchy sequences and pp, then p+qp+q.

    2. Using the first part of this problem, show that if p,p,q,q are Cauchy sequences, pp, and qq, then p+qp+q. You can use the fact that addition on the real numbers is commutative.

  15. 表明如果(A1,B1)(A2,B2)是戴德金割集,那么(A1,B1)+(A2,B2)也是Dedekind切割。

  16. Show that if (A1,B1) and (A2,B2) are Dedekind cuts, then (A1,B1)+(A2,B2) is also a Dedekind cut.

22.无限

22. The Infinite

22.1.等量性

22.1. Equinumerosity

记得在第 20 章中我们定义过,对于每个自然数n,集合[n]={0,1,,n1}。然后我们说一个集合A如果存在双射,则为有限A[n]对于一些n如果一个集合不是有限的,则称其为无限的。

Remember that in Chapter 20 we defined, for each natural number n, the set [n]={0,1,,n1}. We then said that a set A is finite if there is a bijection between A and [n] for some n. A set is said to be infinite if it is not finite.

如果AB是两个有限集,那么当且仅当它们之间存在双射时,它们才具有相同的基数。事实证明,即使AB不是有限的。

If A and B are two finite sets, then they have the same cardinality if and only if there is a bijection between them. It turns out that the same notion of “having the same cardinality” makes sense even if A and B are not finite.


定义。两个集合AB被称为等量,写为AB,如果它们之间存在双射。同样地,我们说AB 具有相同的基数

Definition. Two sets A and B are said to be equinumerous, written AB, if there is a bijection between them. Equivalently, we say that A and B have the same cardinality.


在这个阶段,说AB具有相同的基数可能听起来很奇怪,因为不清楚是否存在任何对象,“基数A”,他们俩都“有”。事实证明,在集合论基础中,有一些对象——自然数的泛化——可以用来测量无限集的大小。它们被称为“基数”或“基数”。但它们与我们的目的无关。在本章的其余部分,当我们说AB具有相同的基数,我们的意思是它们之间存在双射。

At this stage, saying that A and B have the same cardinality may sound strange, because it is not clear that there is any object, “the cardinality of A,” that they both “have.” It turns out that, in set-theoretic foundations, there are certain objects—generalizations of the natural numbers—that one can use to measure the size of an infinite set. There are known as the “cardinal numbers” or “cardinals.” But they are irrelevant to our purposes here. For the rest of this chapter, when we say that A and B have the same cardinality, we mean neither more nor less than the fact that there is a bijection between them.

以下定理本质上说,等数性是一种等价关系。(需要注意的是,到目前为止,我们只讨论了集合之间的关系,所有集合的集合本身并不是一个集合。)

The following theorem says, essentially, that equinumerosity is an equivalence relation. (The caveat is that so far we have spoke only of relations between sets, and the collection of all sets is not itself a set.)


命题。设AB, 和C是任意集合。

Proposition. Let A, B, and C be any sets.

  • AA

  • AA.

  • 如果AB, 然后BA

  • If AB, then BA.

  • 如果ABBC然后AC

  • If AB and BC then AC.


证明留作练习。

The proof is left as an exercise.

22.2.可数无限集

22.2. Countably Infinite Sets

自然数集,N是无限集的典型例子。为了看出它是无限的,另一方面,假设它是有限的。这意味着存在一个双射f之间N[n]对于某个自然数n. 我们可以限制到子集[n+1]N,从而得到一个单射映射[n+1][n]. 但这违反了第 20 章证明的鸽巢原理。

The set of natural numbers, N, is a prototypical example of an infinite set. To see that it is infinite, suppose, on the other hand, that it is finite. This means that there is a bijection f between N and [n] for some natural number n. We can restrict to the subset [n+1] of N, and thereby obtain an injective map from [n+1] to [n]. But this violates the pigeonhole principle, proved in Chapter 20.


定义。如果一个集合等数于N如果一个集合是有限的或者可数无限的,则称其为可数集合。

Definition. A set is said to be countably infinite if it is equinumerous with N. A set is said to be countable if it is finite or countably infinite.


由于恒等映射id(x)=x是任意集合上的双射,每个集合都与其自身等数,因此N本身是可数无限的。

Since the identity map id(x)=x is a bijection on any set, every set is equinumerous with itself, and thus N itself is countably infinite.

“可数无限”这个术语意在引起共鸣。假设A是可数集。根据定义,存在一个双射f:NA。 所以A有一个“第一”元素f(0),第二个元素f(1),第三元素f(2)等等。由于f对于每个元素来说,都是双射aAan以这种方式枚举的元素,其唯一值为n也就是说,每个元素A在某个有限阶段被“计算”。

The term “countably infinite” is meant to be evocative. Suppose A is a countable set. By definition, there is a bijection f:NA. So A has a “first” element f(0), a “second” element f(1), a “third” element f(2), and so on. Since f is a bijection, for every element a of A, a is the nth element enumerated in this way, for a unique value of n. That is, each element of A is “counted” at some finite stage.

有了这个定义,我们自然会想知道我们最喜欢的哪些集合是可数的。整数集Z可数吗?有理数集呢Q或实数集R此时,你应该反思一下这句话的逻辑形式“A是可数的”,并思考如何证明一个集合A是否具有此属性。

With this definition in hand, it is natural to wonder which of our favorite sets are countable. Is the set of integers Z countable? How about the set of rationals Q, or the set of reals R? At this point, you should reflect on the logical form of the statement “A is countable,” and think about what is required to show that a set A does or does not have this property.


定理。整数集,Z,是可数的。

Theorem. The set of integers, Z, is countable.

证明。我们需要证明NZ。 定义f:NZ如下:

Proof. We need to show that there exists a bijection between N and Z. Define f:NZ as follows:

f(n)={n/2if n is even(n+1)/2if n is odd.

我们主张f是双射。为了证明它是单射,假设f(m)=f(n)。 如果f(m)(因此也f(n)) 非负,则mn是偶数,在这种情况下m/2=n/2暗示m=n。 否则,mn很奇怪,而且(m+1)/2=(n+1)/2暗示m=n

We claim that f is a bijection. To see that it is injective, suppose f(m)=f(n). If f(m) (and hence also f(n)) is nonnegative, then m and n are even, in which case m/2=n/2 implies m=n. Otherwise, m and n are odd, and again (m+1)/2=(n+1)/2 implies m=n.

看到这一点f是全射,假设a是任意整数。如果a是非负的,那么a=f(2a)。 如果a是严格负的,那么2a1也严格为负,因此(2a1)是一个奇数。在这种情况下,不难检查a=f((2a1))

To see that f is surjective, suppose a is any integer. If a is nonnegative, then a=f(2a). If a is strictly negative, then 2a1 is also strictly negative, and hence (2a1) is an odd natural number. In that case, it is not hard to check that a=f((2a1)).


我们现在将建立一个定理库,用来证明各种集合都是可数的。

We will now build up an arsenal of theorems that we can use to show that various sets are countable.


定理。一个集合A可数当且仅当A为空或存在全射函数f:NA

Theorem. A set A is countable if and only if A is empty or there is a surjective function f:NA.

证明。对于正向,假设A是可数的。那么它要么是有限的,要么是可数无限的。如果A是可数无限的,有一个双射NA,我们就完成了。那么假设A是有限的。如果A为空,我们就完成了。否则,对于某些n,存在双射f:[n]A, 和n1. 定义一个函数g:NA如下:

Proof. For the forward direction, suppose A is countable. Then it is either finite or countably infinite. If A is countably infinite, there is a bijection from N to A, and we are done. Suppose, then, that A is finite. If A is empty, we are done. Otherwise, for some n, there is a bijection f:[n]A, with n1. Define a function g:NA as follows:

g(i)={f(i)if i<nf(0)otherwise.

换句话说,g列举元素A通过使用f首先,然后重复元素f(0)。 清楚地f正如所要求的,是全射。

In other words, g enumerates the elements of A by using f first, and then repeating the element f(0). Clearly f is surjective, as required.

在另一个方向上,如果A是有限的,那么它就是可数的,我们就完成了。所以假设A不是有限的。那么它就不是空的,所以有一个全射函数f:NA我们需要转向f变成双射函数。问题是f可能不是单射,也就是说,元素A可能会被多次枚举。解决方法是定义一个函数,g,这样可以消除所有重复项。其思路是g应该列举元素f(0),f(1),f(2),,但跳过已经列举的那些。

In the other direction, if A is finite, then it is countable, and we are done. So suppose A is not finite. Then it is not empty, and so there is a surjective function f:NA. We need to turn f into a bijective function. The problem is that f may not be injective, which is to say, elements in A may be enumerated more than once. The solution is to define a function, g, which eliminates all the duplicates. The idea is that g should enumerate the elements f(0),f(1),f(2),, but skip over the ones that have already been enumerated.

准确地说,函数g递归定义如下:g(0)=f(0),并且对于每一个ig(i+1)=f(j), 在哪里j是最小自然数,满足f(j)不属于{g(0),g(1),g(2),,g(i)}. 假设A是无限的,并且f是全射保证某些这样的j始终存在。

To be precise, the function g is defined recursively as follows: g(0)=f(0), and for every i, g(i+1)=f(j), where j is the least natural number such that f(j) is not among {g(0),g(1),g(2),,g(i)}. The assumption that A is infinite and f is surjective guarantees that some such j always exists.

我们只需要检查g是双射。根据定义,对于每个ig(i+1)不同于g(0),,g(i).这意味着g是单射。但我们也可以通过归纳法证明,对于每个i{g(0),,g(i)}{f(0),,f(i)}。 自从f是全射,g也是。

We only need to check that g is a bijection. By definition, for every i, g(i+1) is different from g(0),,g(i). This implies that g is injective. But we can also show by induction that for every i, {g(0),,g(i)}{f(0),,f(i)}. Since f is surjective, g is too.


以类似于我们证明整数可数的方式,我们可以证明以下内容:

In a manner similar to the way we proved that the integers are countable, we can prove the following:


定理。如果AB是可数无限的,那么AB

Theorem. If A and B are countably infinite, then so is AB.

证明。假设f:NAg:NB是全射。然后我们可以定义一个函数h:NAB

Proof. Suppose f:NA and g:NB are surjective. Then we can define a function h:NAB:

h(n)={f(n/2)if n is eveng((n1)/2)if n is odd.

不难证明h是全射。

It is not hard to show that h is surjective.


直观地看,如果A={f(0),f(1),f(2),}B={g(0),g(1),g(2),},然后我们可以枚举AB作为{f(0),g(0),f(1),g(1),f(2),g(2),}

Intuitively, if A={f(0),f(1),f(2),} and B={g(0),g(1),g(2),}, then we can enumerate AB as {f(0),g(0),f(1),g(1),f(2),g(2),}.

接下来的两个定理也很有帮助。第一个定理说,为了证明一个集合B是可数的,用可数集合中的全射函数“覆盖”它就足够了。第二条规则是,为了证明一个集合A是可数的,那么将其嵌入可数集合就足够了。

The next two theorems are also helpful. The first says that to show that a set B is countable, it is enough to “cover” it with a surjective function from a countable set. The second says that to show that a set A is countable, then it is enough to embed it in a countable set.


定理。如果A是可数的,并且f:AB是全射,那么B是可数的。

Theorem. If A is countable and f:AB is surjective, then B is countable.

证明。如果A是可数的,那么就有一个全射函数g:NA, 和fg是一个来自的全射函数NB

Proof. If A is countable, then there is a surjective function g:NA, and fg is a surjective function from NB.

定理。如果B是可数的,并且f:AB是单射,那么A是可数的。

Theorem. If B is countable and f:AB is injective, then A is countable.

证明。假设f:AB是单射,它有一个左逆,g:BA。 自从g有一个右逆,f,我们知道g是全射,我们可以应用前面的定理。

Proof. Assuming f:AB is injective, it has a left inverse, g:BA. Since g has a right inverse, f, we know that g is surjective, and we can apply the previous theorem.

推论。如果B是可数的,并且AB, 然后A是可数的。

Corollary. If B is countable and AB, then A is countable.

证明。函数f:AB定义为f(x)=x是单射。

Proof. The function f:AB defined by f(x)=x is injective.


记住N×N是有序对的集合(i,j)在哪里ij是自然数。

Remember that N×N is the set of ordered pairs (i,j) where i and j are natural numbers.


定理。 N×N是可数的。

Theorem. N×N is countable.

证明。列举元素如下:

Proof. Enumerate the elements as follows:

(0,0),(1,0),(0,1),(2,0),(1,1),(1,2),(3,0),(2,1),(1,2),(0,3),

如果你把这些对看作x-y平面上,对子沿对角线枚举:首先是对角线,其元素总和为0,则对角线的元素对之和为1等等。这通常被称为“燕尾榫”论证,因为如果你想象画一条线来回穿插以这种方式枚举的对,它将类似于木匠使用燕尾榫连接两块木头的做法。(而这个术语又来自于与鸽子尾巴的相似性。)

If you think of the pairs as coordinates in the x-y plane, the pairs are enumerated along diagonals: first the diagonal with pairs whose elements sum to 0, then the diagonal with pairs whose elements sum to 1, and so on. This is often called a “dovetailing” argument, because if you imagine drawing a line that weaves back and forth through the pairs enumerated this ways, it will be analogous to the a carpenter’s practice of using a dovetail to join two pieces of wood. (And that term, in turn, comes from the similarity to a dove’s tail.)

就证明而言,上述非正式描述和相关图表非常令人信服。可以描述N×N但是,要明确地用代数形式来表达。练习中要求你这样做。

As far as proofs go, the informal description above and the associated diagram are perfectly compelling. It is possible to describe a bijection between N×N explicitly, however, in algebraic terms. You are asked to do this in the exercises.

先前的定理有许多有趣的推论。

The previous theorem has a number of interesting consequences.


定理。如果AB是可数的,那么也是A×B

Theorem. If A and B are countable, then so is A×B.

证明。如果pN×N, 写p0p1表示两个分量。设f:NN×N是一个满射,正如前面定理所保证的那样。假设g:NAh:NB是单射。那么函数k(i)=(g(f(i)0),h(f(i)1))是一个来自的全射函数NA×B

Proof. If p is any element of N×N, write p0 and p1 to denote the two components. Let f:NN×N be a surjection, as guaranteed by the previous theorem. Suppose g:NA and h:NB be surjective. Then the function k(i)=(g(f(i)0),h(f(i)1)) is a surjective function from N to A×B.

定理。有理数集,Q,是可数的。

Theorem. The set of rational numbers, Q, is countable.

证明。根据前面的定理,我们知道Z×Z是可数的。定义f:Z×ZQ经过

Proof. By the previous theorem, we know that Z×Z is countable. Define f:Z×ZQ by

f(i,j)={i/jif j00otherwise.

由于Q可以写成i/j对于一些ijZf是全射。

Since every element of Q can be written as i/j for some i and j in Z, f is surjective.

定理。假设A是可数的。对于每个n,集合An是可数的。

Theorem. Suppose that A is countable. For each n, the set An is countable.

证明。记住,我们可以确定n-来自的元素元组AA××A,其中n副本A在产品中。结果如下使用归纳法n

Proof. Remember that we can identify the set of n-tuples of elements from A with A××A, where there are n copies of A in the product. The result follows using induction on n.

定理。(Ai)iN是一个由自然数索引的集合族,假设每个Ai是可数的。那么iAi是可数的。

Theorem. Let (Ai)iN be a family of sets indexed by the natural numbers, and suppose that each Ai is countable. Then iAi is countable.

证明。假设每个ifi是一个来自的全射函数NAi.然后函数g(i,j)=fi(j)是一个来自的全射函数N×NiAi

Proof. Suppose for each i, fi is a surjective function from N to Ai. Then the function g(i,j)=fi(j) is a surjective function from N×N to iAi.

定理。假设A是可数的。那么元素的有限序列集A是可数的。

Theorem. Suppose that A is countable. Then the set of finite sequences of elements of A is countable.

证明。元素的有限序列集A等于iAi,并且我们可以应用前两个定理。

Proof. The set of finite sequences of elements of A is equal to iAi, and we can apply the previous two theorems.


请注意,所有字母数字字符和标点符号的集合(例如,表示为所有 ASCII 字符的集合)是有限的。结合最后一个定理,这意味着英语中只有可数的句子(实际上,任何句子由从任何可数库存中选择的有限符号序列表示的语言中也只有可数的句子)。

Notice that the set of all alphanumeric characters and punctuation (say, represented as the set of all ASCII characters) is finite. Together with the last theorem, this implies that there are only countably many sentences in the English language (and, indeed, any language in which sentences are represented by finite sequences of symbols, chosen from any countable stock).

在这个阶段,似乎一切都是可数的。在下一节中,我们将看到事实并非如此:实数集R,不可数,并且如果A是任意集合(有限或无限),幂集AP(A),不等于A

At this stage, it might seem as though everything is countable. In the next section, we will see that this is not the case: the set of real numbers, R, is not countable, and if A is any set (finite or infinite), the powerset of A, P(A), is not equinumerous with A.

22.3.康托定理

22.3. Cantor’s Theorem

一套A如果不可数,则不可数。我们的目标是证明以下由乔治·康托提出的定理。

A set A is uncountable if it is not countable. Our goal is to prove the following theorem, due to Georg Cantor.


定理。实数集是不可数的。

Theorem. The set of real numbers is uncountable.

证明。记住[0,1]表示闭区间{rR0r1}足以证明不存在射射函数f:N[0,1],因为如果R是可数的,[0,1]也是可数的。

Proof. Remember that [0,1] denotes the closed interval {rR0r1}. It suffices to show that there is no surjective function f:N[0,1], since if R were countable, [0,1] would be countable too.

回想一下,每个实数r[0,1]具有以下形式的十进制展开式r=0.r0r1r2r3r4,其中每个ri{0,1,,9}更正式地,我们可以写成r=i=0ri10i对于每个rR0r1

Recall that every real number r[0,1] has a decimal expansion of the form r=0.r0r1r2r3r4, where each ri is a digit in {0,1,,9}. More formally, we can write r=i=0ri10i for each rR with 0r1.

(请注意1可以写成0.9999.一般来说,[0,1]将有两种这种形式的表示;例如,0.5=0.5000=0.49999。为了具体起见,对于这些数字,我们可以选择以零结尾的表示形式。)

(Notice that 1 can be written 0.9999. In general every other rational number in [0,1] will have two representations of this form; for example, 0.5=0.5000=0.49999. For concreteness, for these numbers we can choose the representation that ends with zeros.)

因此,我们可以写

As a result, we can write

  • f(0)=0.r00r10r20r30r40

  • f(0)=0.r00r10r20r30r40

  • f(1)=0.r01r11r21r31r41

  • f(1)=0.r01r11r21r31r41

  • f(2)=0.r02r12r22r32r42

  • f(2)=0.r02r12r22r32r42

  • f(3)=0.r03r13r23r33r43

  • f(3)=0.r03r13r23r33r43

  • f(4)=0.r04r14r24r34r44

  • f(4)=0.r04r14r24r34r44

(我们使用上标,ri,表示f(i)。上标不代表“i次方。”)

(We use superscripts, ri, to denote the digits of f(i). The superscripts do not mean the “ith power.”)

我们的目标是证明f不是单射。为此,定义一个新的数字序列(ri)iN经过

Our goal is to show that f is not surjective. To that end, define a new sequence of digits (ri)iN by

ri={7if rii73otherwise.

定义实数r=0.r0r1r2r3.然后,对于每个ir不同于f(i)i第一位数字。但这意味着对于每个if(i)r。 自从r不在范围内f,我们看到f不是单射。因为f是任意的,没有来自N[0,1]

The define the real number r=0.r0r1r2r3. Then, for each i, r differs from f(i) in the ith digit. But this means that for every i, f(i)r. Since r is not in the range of f, we see that f is not surjective. Since f was arbitrary, there is no surjective function from N to [0,1].

(我们选择数字37只能避免09,以避免出现以下情况,例如,f(0)=0.5000r=0.4999. 由于没有零或九r,因为i第一位数字r不同于f(i),它确实是一个不同的实数。)

(We chose the digits 3 and 7 only to avoid 0 and 9, to avoid the case where, for example, f(0)=0.5000 and r=0.4999. Since there are no zeros or nines in r, since the ith digit of r differs from f(i), it really is a different real number.)


这个引人注目的证明被称为“对角化论证”。我们试图构造一个具有特定属性的实数,即它不在f我们制作一个数字表,其中的行代表我们必须满足的无限多的约束(即,对于每个if(i)r),而列代表满足该约束的机会(即通过选择i第一位数字r然后我们沿着对角线完成构造,使用i第一次机会来满足i约束。该技术经常用于逻辑和可计算性理论。

This remarkable proof is known as a “diagonalization argument.” We are trying to construct a real number with a certain property, namely, that it is not in the range of f. We make a table of digits, in which the rows represent infinitely many constraints we have to satisfy (namely, that for each i, f(i)r), and the columns represent opportunities to satisfy that constraint (namely, by choosing the ith digit of r appropriately). Then we complete the construction by stepping along the diagonal, using the ith opportunity to satisfy the ith constraint. This technique is used often in logic and computability theory.

下面提供了不可数集的另一个例子。

The following provides another example of an uncountable set.


定理。自然数的幂集,P(N),是不可数的。

Theorem. The power set of the natural numbers, P(N), is uncountable.

证明。f:NP(N)是任意函数。我们的目标是再次证明f不是单射。设S是自然数集合,定义如下:

Proof. Let f:NP(N) be any function. Once again, our goal is to show that f is not surjective. Let S be the set of natural numbers, defined as follows:

S={nNnf(n)}.

换句话说,对于每个自然数,nn位于S当且仅当它不在f(n).那么显然对于每一个nf(n)S。 所以f不是单射。

In words, for every natural number, n, n is in S if and only if it is not in f(n). Then clearly for every n, f(n)S. So f is not surjective.


我们也可以将其视为对角化论证:绘制一个表格,行和列均由自然数索引,其中i第行和j第列为“是”,如果j是...的一个元素f(i)否则为“否”。集合S是通过沿对角线交换“是”和“否”条目构建的。

We can also view this as a diagonalization argument: draw a table with rows and columns indexed by the natural numbers, where the entry in the ith row and jth column is “yes” if j is an element of f(i), and “no” otherwise. The set S is constructed by switching “yes” and “no” entries along the diagonal.

事实上,完全相同的论证可以得出以下结论:

In fact, exactly the same argument yields the following:


定理。对于每个集合A,没有来自的全射函数AP(A)

Theorem. For every set A, there is no surjective function from A to P(A).

证明。如上所述,如果f是来自的任何函数AP(A),集合S={aAaf(a)}不在范围内f

Proof. As above, if f is any function from A to P(A), the set S={aAaf(a)} is not in the range of f.


这表明无穷级数是无穷的。例如,在序列N,P(N),P(P(N)),,存在一个将每个集合映射到下一个集合的单射函数,但没有单射函数。所有这些集合的并集甚至更大,然后我们可以取的幂集,依此类推。集合论学家至今仍在研究这个层次结构。

This shows that there is an endless hierarchy of infinities. For example, in the sequence N,P(N),P(P(N)),, there is an injective function mapping each set into the next, but no surjective function. The union of all those sets is even larger still, and then we can take the power set of that, and so on. Set theorists are still today investigating the structure within this hierarchy.

22.4.有限性的另一种定义

22.4. An Alternative Definition of Finiteness

无限与有限的区别之一是,无限集可以与其本身的真子集大小相同。例如,自然数、偶数集和完全平方集都是等数集,尽管后两者严格包含在自然数中。

One thing that distinguishes the infinite from the finite is that an infinite set can have the same size as a proper subset of itself. For example, the natural numbers, the set of even numbers, and the set of perfect squares are all equinumerous, even though the latter two are strictly contained among the natural numbers.

19 世纪,数学家理查德·戴德金 (Richard Dedekind) 使用这一奇特的性质来定义有限性。我们可以证明他的定义与我们的定义等价,但证明需要选择公理。

In the nineteenth century, the mathematician Richard Dedekind used this curious property to define what it means to be finite. We can show that his definition is equivalent to ours, but the proof requires the axiom of choice.


定义。集合是A Dedekind 无限如果A与自身的真子集等数,否则为戴德金有限

Definition. A set is A Dedekind infinite if A is equinumerous with a proper subset of itself, and Dedekind finite otherwise.

定理。一个集合是戴德金无限的当且仅当它是无限的。

Theorem. A set is Dedekind infinite if and only it is infinite.

证明。假设A是戴德金无限的。我们需要证明它不是有限的;相反,假设它是双射的[n]对于一些n。合成双射,我们有[n]是与自身真子集双射的。这意味着存在一个单射函数f[n]到的真子集n. 修改f,我们可以得到一个注入函数[n]进入[n1],与鸽巢原理相矛盾。

Proof. Suppose A is Dedekind infinite. We need to show it is not finite; suppose, to the contrary, it is bijective with [n] for some n. Composing bijections, we have that [n] is bijective with a proper subset of itself. This means that there is an injective function f from [n] to a proper subset of n. Modifying f, we can get an injective function from [n] into [n1], contradicting the pigeonhole principle.

另一方面,假设A是无限的。我们需要证明有一个单射函数fA到其自身的真子集(因为f是之间的双射A以及f). 选择不同元素序列a0,a1,a2,A。 让f地图各aiai+1,但保留其他所有元素A已修复。然后f是单射,但是a0不在范围内f,按要求。

Suppose, on the other hand, that A is infinite. We need to show that there is an injective function f from A to a proper subset of itself (because then f is a bijection between A and the range of f). Choose a sequence of distinct element a0,a1,a2, of A. Let f map each ai to ai+1, but leave every other element of A fixed. Then f is injective, but a0 is not in the range of f, as required.


22.5.康托-伯恩斯坦定理

22.5. The Cantor-Bernstein Theorem

AB直观地看,是等量数的意思AB大小相同。还有一种自然的说法是A不大于B

Saying that A and B are equinumerous means, intuitively, that A and B have the same size. There is also a natural way of saying that A is not larger than B:


定义。对于两个集合AB,我们说基数A小于或等于基数B,书面AB,当有注射时f:AB

Definition. For two sets A and B, we say the cardinality of A is less than or equal to the cardinality of B, written AB, when there is an injection f:AB.


作为练习,我们要求你展示预序的,也就是说,它是自反的和传递的。这是一个自然的问题:ABBA意味着AB换句话说,假设存在注入函数f:ABg:BA,是否必然存在AB

As an exercise, we ask you to show that is a preorder, which is to say, it is reflexive and transitive. Here is a natural question: does AB and BA imply AB? In other words, assuming there are injective functions f:AB and g:BA, is there necessarily a bijection from A to B?

答案是“是”,但证明起来很棘手。这个结果被称为康托-伯恩斯坦定理,我们无需证明即可陈述它。

The answer is “yes,” but the proof is tricky. The result is known as the Cantor-Bernstein Theorem, and we state it without proof.


定理。对于任何集合AB, 如果ABBA, 然后AB

Theorem. For any sets A and B, if AB and BA, then AB.


22.6.练习

22.6. Exercises

  1. 证明等量性是自反的、对称的、和传递的。

  2. Show that equinumerosity is reflexive, symmetric, and transitive.

  3. 证明函数f(x)=x/(1x)是区间之间的双射[0,1)R0

  4. Show that the function f(x)=x/(1x) is a bijection between the interval [0,1) and R0.

  5. 表明g(x)=x/(1|x|)给出之间的双射(1,1)R

  6. Show that the g(x)=x/(1|x|) gives a bijection between (1,1) and R.

  7. 定义函数J:N×NN经过J(i,j)=(i+j)(i+j+1)2+i。这个问题的目的是为了证明J是来自的双射N×NN

    1. 画一个图来表明哪些对被发送到0,1,2,

    2. n=i+j. 表明J(i,j)等于对数(u,v)如此一来u+v<n, 或者u+v=nu<i.(利用这一事实1+2++n=n(n+1)/2

    3. 结论J是单射:找到ij使得J(i,j)=k,只要找到最大的n使得n(n+1)/2k, 让i=kn(n+1)/2,并让j=ni

    4. 结论J是单射:如果J(i,j)=J(i,j), 让n=i+jn=i+j. 认为n=n, 所以i=ij=j

  8. Define a function J:N×NN by J(i,j)=(i+j)(i+j+1)2+i. This goal of this problem is to show that J is a bijection from N×N to N.

    1. Draw a picture indicating which pairs are sent to 0,1,2,.

    2. Let n=i+j. Show that J(i,j) is equal the number of pairs (u,v) such that either u+v<n, or u+v=n and u<i. (Use the fact that 1+2++n=n(n+1)/2.)

    3. Conclude that J is surjective: to find i and j such that J(i,j)=k, it suffices to find the largest n such that n(n+1)/2k, let i=kn(n+1)/2, and let j=ni.

    4. Conclude that J is injective: if J(i,j)=J(i,j), let n=i+j and n=i+j. Argue that n=n, and so i=i and j=j.

  9. S是来自的函数集N{0,1}. 使用对角论证来证明S是不可数的。(请注意,你可以想象一个函数f:N{0,1}作为 0 和 1 的无限序列,由下式给出f(0),f(1),f(2),.因此,给定一个函数F(n)对于每个自然数n,返回一个由 0 和 1 组成的无限序列,你需要找到一个不在图像中的序列F

  10. Let S be the set of functions from N to {0,1}. Use a diagonal argument to show that S is uncountable. (Notice that you can think of a function f:N{0,1} as an infinite sequence of 0’s and 1’s, given by f(0),f(1),f(2),. So, given a function F(n) which, for each natural number n, returns an infinite sequence of 0’s and 1’s, you need to find a sequence that is not in the image of F.)

  11. 如果fg是来自的函数NN,说g 最终占据主导地位 f如果有的话n对于每一个mng(m)>f(m)换句话说,从某个时刻开始,g大于f

    表明如果f0,f1,f2,是来自的任意函数序列NN,以自然数为指标,则有一个函数g最终主宰每一个fi. (提示:构造g因此对于每一个ig(n)>fi(n)对于每一个ni

  12. If f and g are functions from N to N, say that g eventually dominates f if there is some n such that for every mn, g(m)>f(m). In other words, from some point on, g is bigger than f.

    Show that if f0,f1,f2, is any sequence of functions from N to N, indexed by the natural numbers, then there is a function g that eventually dominates each fi. (Hint: construct g so that for each i, g(n)>fi(n) for every ni.)

  13. 表明关系在第 22.5 节中定义是自反的和传递的。

  14. Show that the relation defined in Section 22.5 is reflexive and transitive.

23.公理基础

23. Axiomatic Foundations

在这最后一章中,我们的故事又回到了原点。我们从符号逻辑开始我们的旅程,使用命题连接词来模拟“与”、“或”、“非”和“蕴含”等逻辑术语。在此基础上,我们添加了一阶逻辑的量词、函数和关系符号。从那里,我们转向集合、函数和关系,它们在现代数学中无处不在;自然数和归纳法;然后是数论、组合学、实数和无穷大等主题。在这里,我们回到符号逻辑,看看它如何用于为所有数学提供形式基础。

In this final chapter, our story comes full circle. We started our journey with symbolic logic, using the propositional connectives to model logical terms like “and,” “or,” “not,” and “implies.” To that we added the quantifiers and function and relation symbols of first-order logic. From there, we moved to sets, functions, and relations, which are ubiquitous in modern mathematics; the natural numbers and induction; and then topics such as number theory, combinatorics, the real numbers, and the infinite. Here we return to symbolic logic, and see how it can be used to provide a formal foundation for all of mathematics.

具体来说,我们将考虑一个公理框架,即Zermelo-Fraenkel 集合论,它是在 20 世纪初引入的。在数学的集合论观点中,每个数学对象都是一个集合。公理断言具有各种属性的集合的存在。从所有集合的集合中,我们挖掘出数学宇宙的常见成员,不仅仅是我们考虑过的各种数字系统,还包括对、有限序列、关系、函数等等。这为我们自第 11 章以来所做的一切提供了理想化的基础。

Specifically, we will consider an axiomatic framework known as Zermelo-Fraenkel set theory, which was introduced early in the twentieth century. In the set-theoretic view of mathematics, every mathematical object is a set. The axioms assert the existence of sets with various properties. From the collection of all sets, we carve out the usual inhabitants of the mathematical universe, not just the various number systems we have considered, but also pairs, finite sequences, relations, functions, and so on. This provides us with an idealized foundation for everything we have done since Chapter 11.

在本章的最后,我们将简要介绍另一个公理框架,即 Lean 所使用的依赖类型理论。我们将看到,它为数学对象和构造提供了另一种视角,但仍然可以与集合论观点相互解释。

At the end of this chapter, we will briefly describe another axiomatic framework, dependent type theory, which is the one used by Lean. We will see that it provides an alternative perspective on mathematical objects and constructions, but one which is nonetheless inter-interpretable with the set-theoretic point of view.

23.1.集合的基本公理

23.1. Basic Axioms for Sets

集合论的公理用一阶逻辑来表达,对于具有单个二元关系符号的语言,我们认为整个数学宇宙只由集合组成;如果xy是集合,我们可以表示x是...的一个元素y通过写作xy。第一条公理说,两个集合相等当且仅当它们具有相同的元素。

The axioms of set theory are expressed in first-order logic, for a language with a single binary relation symbol, . We think of the entire mathematical universe as consisting of nothing but sets; if x and y are sets, we can express that x is an element of y by writing xy. The first axiom says that two sets are equal if and only if they have the same elements.

Extensionality:x,y(x=yz(zxzy))

下一个公理告诉我们,宇宙中至少有一个有趣的集合,即没有元素的集合。

The next axiom tells us that there is at least one interesting set in the universe, namely, the set with no element.

Empty set:xyyx

当然,xy缩写¬(xy)根据外延性公理,该公理断言存在的集合是唯一的:换句话说,如果x1x2每一个都没有元素,那么,从空义上讲,任何元素在一个中当且仅当它在另一个中,所以x1=x2。这证明了在短语“空集”中使用单词“the”是合理的。鉴于这一事实,引入一个新符号似乎是无害的,,表示与该描述相匹配的集合。事实上,我们可以证明这是这种情况:从精确的意义上讲,这种对一阶语言的扩展可以看作是一种方便的表达方式,而更大语言中的语句可以翻译成原始语言,以证明所有预期的推论。我们不会在这里详细讨论,而是认为这一事实是理所当然的。使用新的符号,空集公理告诉我们空集满足属性yy

Here, of course, xy abbreviates ¬(xy). By the axiom of extensionality, the set asserted to exist by this axiom is unique: in other words, if x1 and x2 each have no elements, then, vacuously, any element is in one if and only if it is in the other, so x1=x2. This justifies using the word the in the phrase the empty set. Given this fact, it should seem harmless to introduce a new symbol, , to denote the set matching that description. Indeed, one can show that this is case: in a precise sense, such expansions to a first-order language can be viewed as nothing more than a convenient manner of expression, and statements in the bigger language can be translated to the original language in a way that justifies all the expected inferences. We will not go into the details here, and, rather, take this fact for granted. Using the new symbol, the empty set axiom tells us the empty set satisfies the property yy.

第三条公理告诉我们,给定两个集合xy,我们可以形成一个新的集合z其元素恰好xy

The third axiom tells us that given two sets x and y, we can form a new set z whose elements are exactly x and y.

Pairing:x,yzw(wzw=xw=y)

这条公理有一个隐秘的用法。这条公理不需要xy是不同的,因此,例如,我们可以将它们都视为空集。这告诉我们集合{},其唯一元素是空集,存在。更一般地,公理告诉我们,对于任何x,我们有集合{x}其唯一元素是x,并且对于任何xy,我们有{x,y},如上所述。外延公理再次告诉我们,满足这些描述的集合是唯一的,因此使用相应的符号是公平的。我们现在开始行动了!我们现在拥有以下所有集合,以及更多:

There is a stealth usage of this axiom lurking nearby. The axiom does not require that x and y are different, so, for example, we can take them both to be the empty set. This tells us that the set {}, whose only element is the empty set, exists. More generally, the axiom tells us that for any x, we have the set {x} whose only element is x, and for any x and y, we have {x,y}, as described above. Once again, the axiom of extensionality tells us that the sets meeting these descriptions are unique, so it is fair to use the corresponding notation. We are now off and running! We now have all of the following sets, and more:

,{},{{}},{,{}},{{{}}},

尽管如此,我们永远无法以这种方式形成一个包含两个以上元素的集合。为此,合理的做法是添加一条公理,该公理断言对于每个xy,集合xy存在。但我们可以做得更好。请记住,如果x是任意集合,x表示所有集合的并集x换句话说,对于任何集合zz是...的一个元素x当且仅当z位于w对于某个集合wx。以下公理断言该集合存在。

Still, we can never form a set with more than two elements in this way. To that end, it would be reasonable to add an axiom that asserts for every x and y, the set xy exists. But we can do better. Remember that if x is any set, x denotes the union of all the sets in x. In other words, for any set z, z is an element of x if and only if z is in w for some set w in x. The following axiom asserts that this set exists.

Union:xyz(zyw(wxzw))

再次证明,使用符号。我们利用这个公理和配对得到普通的二元并集,因为我们有xy={x,y}

Once again, this justifies the use of the notation. We get the ordinary binary union using this axiom together with pairing, since we have xy={x,y}.

在这个阶段,调用我们在集合的非正式表示中首次引入的一些附加符号将很有用。如果A是集合论语言中的任何一阶公式,xyA缩写x(xyA)xyA缩写x(xyA),按照第 7.4 节中的描述对量词进行相对化。表达式xy缩写zx(zy),正如您所期望的。

At this stage, it will be useful to invoke some additional notation that was first introduced in our informal presentation of sets. If A is any first-order formula in the language of set theory, xyA abbreviates x(xyA) and xyA abbreviates x(xyA), relativizing the quantifiers as described in Section 7.4. The expression xy abbreviates zx(zy), as you would expect.

下一个公理断言,对于每个集合x,幂集,P(x)存在。

The next axiom asserts that for every set x, the power set, P(x) exists.

Power Set:xyz(zyzx)

我们已经开始用基本集合构造填充宇宙。然而,正是下一个公理赋予了集合论非凡的灵活性。严格地说,它不是单个公理,而是一个模式,即由单个模板给出的无限公理系列。该模式旨在证明集合构造器符号的合理性{w}这在第 11 章中随处可见。我们需要解决的第一个问题是,我们可以用什么来代替省略号。在我们对集合论的非正式介绍中,我们说过,可以使用任何属性来定义集合,但这只会引出一个问题,即什么算作“属性”。公理集合论提供了一个简单但有力的答案:我们可以使用集合论语言中的任何一阶公式。

We have begun to populate the universe with basic set constructions. It is the next axiom, however, that gives set theory its remarkable flexibility. Properly speaking, it is not a single axiom, but a schema, an infinite family of axioms given by a single template. The schema is meant to justify set-builder notation {w} that was ubiquitous in Chapter 11. The first question we need to address is what we are allowed to write in place of the ellipsis. In our informal presentation of set theory, we said that one can define a set using any property, but that only prompts the question here as to what counts as a “property.” Axiomatic set theory provides a simple but powerful answer: we can use any first-order formula in the language of set theory.

另一个担忧围绕着罗素悖论,如第 11.1 节所述。任何允许我们定义集合的理论{www}是不一致的,因为如果我们称这个集合z,我们可以证明zz当且仅当zz,这是矛盾的。集合论再次提供了一个简单而优雅的解决方案:对于任何公式A(z)并设置y,我们可以形成集合{wyA(w)},由以下元素组成y满足A换句话说,我们必须首先利用集合论的其他公理来形成一个集合y它足够大,可以包含我们想要考虑的所有元素,然后使用公式A挑选出我们想要的。

Another concern centers around Russell’s paradox, as discussed in Section 11.1. Any theory that allows us to define the set {www} is inconsistent, since if we call this set z, we can show zz if and only if zz, which is a contradiction. Once again, set theory offers a simple and elegant solution: for any formula A(z) and set y, we can instead form the set {wyA(w)}, consisting of the elements of y that satisfy A. In other words, we have to first use the other axioms of set theory to form a set y that is big enough to include all the elements that we want to consider, and then use the formula A to pick out the ones we want.

我们想要的公理模式被称为分离,因为我们用它将我们想要的元素从更大的集合中的元素中分离出来。

The axiom schema we want is called separation, because we use it to separate the elements we want from those in a bigger collection.

Separation:x1,x2,,xn,yzw(wzwyA(w,x1,x2,,xn))

这里,A可以是任何公式,以及变量列表x1,,xn所示的公式表明A可以有一些参数,在这种情况下,我们形成的集合取决于这些值。例如,在普通数学中,给定一个数字m我们可以形成集合{nNprime(n)n>m}. 在本例中,描述涉及mn,并且如此定义的集合取决于m

Here, A can be any formula, and the list of variables x1,,xn that are shown indicate that the formula A can have some parameters, in which case the set we form depends on these values. For example, in ordinary mathematics, given a number m we can form the set {nNprime(n)n>m}. In this example, the description involves m and n, and the set so defined depends on m.

我们可以使用分离公理来简化前面的公理。例如,只要我们知道任何集合x存在,我们可以将空集定义为{yx}类似地,在配对公理中,断言存在一个集合包含xy作为元素,因为这样我们就可以用分离法来划分出元素恰好是xy

We could use the separation axiom to simplify the previous axioms. For example, as long as we know that any set x exists, we can define the empty set as {yx}. Similarly, in the pairing axiom, it is enough to assert that there is a set that contains x and y as elements, because then we can use separation to carve out the set whose elements are exactly x and y.

这些只是集合论的前六个公理;我们还有四个要学。但这些公理本身就为推理集合、关系和函数提供了基础,就像我们在第 11 章第 13 章和第 15 章中所做的那样。例如,我们已经定义了并集运算,我们可以定义集合交集xy作为{zxyzxzy}。我们不能定义任意的集合补集;例如,练习要求你证明在集合论中我们可以证明不存在包含所有集合的集合,因此空集的补集不存在。但给定任意两个集合xy,我们可以定义它们的区别xy作为{zxzy}下面的练习要求你证明,一旦我们形成了函数的概念,我们也可以定义索引并集和交集。

These are only the first six axioms of set theory; we have four more to go. But these axioms alone provide a foundation for reasoning about sets, relations, and functions, as we did in Chapter 11, Chapter 13, and Chapter 15. For example, we have already defined the union operation, and we can define set intersection xy as {zxyzxzy}. We cannot define arbitrary set complements; for example, the exercises ask you to show that in set theory we can prove that there is no set that contains all sets, and so the complement of the empty set does not exist. But given any two sets x and y, we can define their difference xy as {zxzy}. The exercises below ask you to show that we can also define indexed unions and intersections, once we have developed the notion of a function.

我们想定义两个集合之间的二元关系xy是……的子集x×y,但我们首先必须定义笛卡尔积x×y。记得在11.4 节中我们定义了有序对(u,v)成为集合{{u},{u,v}}。因此,我们可以使用分离公理来定义

We would like to define a binary relation between two sets x and y to be a subset of x×y, but we first have to define the cartesian product x×y. Remember that in Section 11.4 we defined the ordered pair (u,v) to be the set {{u},{u,v}}. As a result, we can use the separation axiom to define

x×y={zuxvy(z=(u,v))}

只要我们能证明存在一个足够大的集合来填充“……”。在下面的练习中,我们要求你证明集合P(P(xy))包含所有相关的有序对。二元关系rxy那么只是x×y,我们解释r(u,v)作为(u,v)r。我们可以从集合中想到有序三元组xyz作为元素x×(y×z)等等。这给了我们三元关系、四元关系等等。

provided we can prove the existence of a set big enough to fill the “….” In the exercises below, we ask you to show that the set P(P(xy)) contains all the relevant ordered pairs. A binary relation r on x and y is then just a subset of x×y, where we interpret r(u,v) as (u,v)r. We can think of ordered triples from the sets x, y, z as elements of x×(y×z) and so on. This gives us ternary relations, four-place relations, and so on.

现在我们可以说一个函数f:xy实际上是满足ux!vyf(u,v),我们写f(u)=v什么时候v是满足的唯一元素f(u,v). 一个函数f从集合中获取参数xy, 和z并返回w的一个元素可以解释为一个函数f:x×y×zw, 等等。

Now we can say that a function f:xy is really a binary relation satisfying ux!vyf(u,v), and we write f(u)=v when v is the unique element satisfying f(u,v). A function f taking arguments from sets x, y, and z and returning an element of w can be interpreted as a function f:x×y×zw, and so on.

有了集合、关系和函数,我们就有了做数学的基本基础。现在我们只缺少一些有趣的集合和结构来处理。例如,如果有一组自然数就好了,N,具有我们期望它具有的所有属性。接下来让我们讨论一下。

With sets, relations, and functions, we have the basic infrastructure we need to do mathematics. All we are missing at this point are some interesting sets and structures to work with. For example, it would be nice to have a set of natural numbers, N, with all the properties we expect it to have. So let us turn to that next.

23.2.无限公理

23.2. The Axiom of Infinity

利用我们目前掌握的公理,我们可以形成许多有限集,首先是并迭代配对、并集、幂集和分离构造。这将为我们提供如下集合

With the axioms we have so far, we can form lots of finite sets, starting with and iterating pairing, union, powerset, and separation constructions. This will give us sets like

,{},{{}},{,{}},{{{}}},

但迄今为止的公理还不允许我们定义比这些更有趣的集合。特别是,没有一条公理能给出无限集。所以我们需要进一步的公理来告诉我们这样的集合是存在的。

But the axioms so far do not allow us to define sets that are more interesting than these. In particular, none of the axioms gives us an infinite set. So we need a further axiom to tell us that such a set exists.

还记得在第 17 章中,我们将自然数描述为具有独特元素的集合,0以及注入操作succ,满足归纳和递归定义原理。在集合论中,一切都是集合,所以如果我们想在这个框架中表示自然数,我们需要用特定的集合来标识它们。有一个自然的选择0即空集,。对于后续操作,我们将使用函数succ定义为succ(x)=x{x}。这个选择有点儿不妥;这个定义最好的理由是它确实有效。根据这个定义,前几个自然数如下:

Remember that in Chapter 17 we characterized the natural numbers as a set with a distinguished element, 0, and an injective operation succ, satisfying the principles of induction and recursive definition. In set theory, everything is a set, so if we want to represent the natural numbers in that framework, we need to identify them with particular sets. There is a natural choice for 0, namely, the empty set, . For a successor operation, we will use the function succ defined by succ(x)=x{x}. The choice is a bit of a hack; the best justification for the definition is that it works. With this definition, the first few natural numbers are as follows:

0=,1={},2={,{}},3={,{},{,{}}},

更清楚的写法如下:

It is more perspicuous to write them as follows:

0=,1={0},2={0,1},3={0,1,2},4={0,1,2,3},

一般来说,n+1由集合表示{0,1,,n},在这种情况下,mn等同于m<n这只是我们编码的一个偶然属性,但却是一个相当迷人的属性。

In general, n+1 is represented by the set {0,1,,n}, in which case, mn is the same as m<n. This is just an incidental property of our encoding, but it is a rather charming one.

回想一下第 17 章,我们可以将自然数集描述如下:

Recall from Chapter 17 that we can characterize the set of natural numbers as follows:

  • 有一个元素0N并且有一个注入函数succ:NN,并附加以下属性succ(x)0对于任意xN

  • There is an element 0N and there is an injective function succ:NN, with the additional property that succ(x)0 for any x in N.

  • 该套装N满足归纳原理:如果xN包含0并关闭succ(即,每当z位于N,所以succ), 然后x=N

  • The set N satisfies the principle of induction: if x is a subset of N that contains 0 and is closed under succ (that is, whenever z is in N, so is succ), then x=N.

我们已经确定了0succ,但我们还没有找到任何包含第一个元素且在应用第二个元素时封闭的集合。无限公理恰恰断言存在这样的集合。

We have already settled on the definitions of 0 and succ, but we don’t yet have any set that contains the first and is closed under applying the second. The axiom of infinity asserts precisely that there exists such a set.

Infinity:x(xy(yxy{y}x))

说一套x如果它满足存在量词后的属性,即它包含空集并且在我们的后继操作下封闭,那么它就是归纳的。请注意,我们仍在尝试正式定义的自然数集具有此属性。无限公理断言某个归纳集的存在,但不一定是自然数本身;归纳集也可以包含其他东西。从某种意义上说,归纳原理表明自然数是最小的归纳集。因此,我们需要一种方法将该集合与无限公理断言存在的集合区分开来。

Say a set x is inductive if it satisfies the property after the existential quantifier, namely, that it contains the empty set and is closed under our successor operation. Notice that the set of natural numbers, which we are still trying to define formally, has this property. The axiom of infinity asserts the existence of some inductive set, but not necessarily the natural numbers themselves; an inductive set can have other things in it as well. In a sense, the principle of induction says that the natural numbers is the smallest inductive set. So we need a way to separate that set from the one asserted to exist by the axiom of infinity.

x是任意归纳集,正如无限公理所断言的那样。设

Let x be any inductive set, as asserted to exist by the axiom of infinity. Let

y={zxz is inductive}.

这里zx也可以写成zP(x),所以根据分离公理,内集存在。根据这个定义,y是每个归纳子集的交集x,所以一个元素w位于y当且仅当w在每个归纳子集中x.我们声称y本身是归纳的。首先,我们有y,因为空集是每个归纳集的元素。接下来假设w位于y。 然后w在每个归纳子集中x。但由于每个归纳集在后继下都是封闭的,succ(w)在每个归纳子集中x。 所以succ(w)位于所有归纳子集的交集中x—也就是说y

Here zx can also be written zP(x), so the inside set exists by the separation axiom. According to this definition, y is the intersection of every inductive subset of x, so an element w is in y if and only if w is in every inductive subset of x. We claim that y itself is inductive. First, we have y, since the empty set is an element of every inductive set. Next, suppose w is in y. Then w is in every inductive subset of x. But since every inductive set is closed under successor, succ(w) is in every inductive subset of x. So succ(w) is in the intersection of all inductive subsets of x—which is y!

很快,y每个归纳集的子集。为了理解这一点,假设z是归纳性的。你可以检查zx是归纳性的,因此yzxz

It quickly follows that y is a subset of every inductive set. To see this, suppose that z is inductive. You can check that zx is inductive, and thus yzxz.

更有趣的是y也满足归纳原理。为了看到这一点,假设uy包含空集并且封闭于succ。 然后u是归纳性的,因为y是每个归纳集的子集,我们有yu.由于我们假设uy,我们有u=y,这正是我们想要的。

The more interesting point is that y also satisfies the principle of induction. To see this, suppose uy contains the empty set and is closed under succ. Then u is inductive, and since y is a subset of every inductive set, we have yu. Since we assumed uy, we have u=y, which is what we want.

总而言之,我们证明了一个集合的存在,它包含0并且根据后继运算封闭并满足归纳公理。此外,只有一个这样的集合:如果y1y2两者都具有此属性,那么y1y2,根据归纳原理,这个交点必须等于y1y2,在这种情况下y1y2相等。那么将具有这些属性的唯一集合称为自然数,并用符号表示它,这是有意义的N

To summarize, then, we have proved the existence of a set that contains 0 and is closed under a successor operation and satisfies the induction axiom. Moreover, there is only one such set: if y1 and y2 both have this property, then so does y1y2, and by the induction principle, this intersection has to be equal to both y1 and y2, in which case y1 and y2 are equal. It then makes sense to call the unique set with these properties the natural numbers, and denote it by the symbol N.

现在只缺少了一块拼图。从定义中可以清楚地看出0不是任何数的后继,但后继函数是否是单射还不清楚。我们可以证明这一点,首先注意到我们定义的自然数有一个特殊的性质:如果z是自然数,y是...的一个元素z, 和x是...的一个元素y, 然后x是...的一个元素z. 这表明关系在自然数上是传递的,这并不奇怪,因为我们已经注意到在我们的表示下,自然数上的<。为了正式证明这一说法,假设有一组z如果它具有刚才提到属性,即元素的每个元素z是z的一个元素。这相当于说对于每个yz,我们有yz

There is only one piece of the puzzle missing. It is clear from the definition that 0 is not the successor of any number, but it is not clear that the successor function is injective. We can prove that by first noticing that the natural numbers, as we have defined them, have a peculiar property: if z is a natural number, y is an element of z, and x is an element of y, then x is an element of z. This says exactly that the relation is transitive on natural numbers, which is not surprising, since we have noted that on the natural numbers, under our representation, coincides with <. To prove this claim formally, say that a set z is transitive if it has the property just mentioned, namely, that every element of an element of z is an element of z. This is equivalent to saying that for every yz, we have yz.


引理:每个自然数都是可传递的。

Lemma. Every natural number is transitive.

证明。通过对自然数进行归纳。显然,是传递的。假设x是传递的,假设ysucc(x)zy。 自从succ(x)=x{x},我们有yx或者y{x}。 如果yx,然后根据归纳假设,我们有zx,因此zsucc(x)否则,我们有y{x}, 所以y=x。在这种情况下,我们又有zx,因此zsucc(x)

Proof. By induction on the natural numbers. Clearly, is transitive. Suppose x is transitive, and suppose ysucc(x) and zy. Since succ(x)=x{x}, we have yx or y{x}. If yx, then by the inductive hypothesis, we have zx, and hence zsucc(x). Otherwise, we have y{x}, and so y=x. In that case, again we have zx, and hence zsucc(x).


下一个引理表明,在传递集上,并集的作用类似于前任运算。

The next lemma shows that, on transitive sets, union acts like the predecessor operation.


引理。如果x是传递的,那么succ(x)=x

Lemma. If x is transitive, then succ(x)=x.

证明。假设y位于succ(x)=(x{x}). 然后yz对于一些zx, 或者yx在第一种情况下,也有yx, 自从x是传递的。

Proof. Suppose y is in succ(x)=(x{x}). Then either yz for some zx, or yx. In the first case, also have yx, since x is transitive.

相反,假设y位于x。 然后y位于succ(x),因为我们有xsucc(x)

Conversely, suppose y is in x. Then y is in succ(x), since we have xsucc(x).

定理。 succ是可注入的N

Theorem. succ is injective on N.

证明。假设xyN, 和succ(x)=succ(y)。 然后xy都是传递性的,我们有x=succ(x)=succ(y)=y

Proof. Suppose x and y are in N, and succ(x)=succ(y). Then x and y are both transitive, and we have x=succ(x)=succ(y)=y.


这样,我们就可以开始了。虽然我们不会在这里介绍细节,但使用归纳原理,我们可以证明递归定义原理的合理性。然后,我们可以继续定义算术的基本运算并推导它们的属性,如第 17 章中所做的那样。我们可以继续定义整数、有理数和实数,如第 21章中所述,并开发数论和组合学等主题,如第 19 章和第 20 章中所述。事实上,似乎任何合理的数学分支都可以在公理集合论的基础上正式发展。例如,存在与大集合有关的陷阱:例如,正如假设存在一个由所有集合组成的集合是不一致的一样,也不存在所有偏序或所有群的集合。因此,在解释某些数学主张时,在某些情况下必须小心,将其限制在足够大的此类对象集合上。但这几乎只相当于仔细的记账,而且值得注意的是,在大多数情况下,集合论的公理足够灵活和强大,可以证明大多数普通的数学构造。

With that, we are off and running. Although we will not present the details here, using the principle of induction we can justify the principle of recursive definition. We can then go on to define the basic operations of arithmetic and derive their properties, as done in Chapter 17. We can go on to define the integers, the rational numbers, and the real numbers, as described in Chapter Chapter 21, and to develop subjects like number theory and combinatorics, as described in Chapters Chapter 19 and Chapter 20. In fact, it seems that any reasonable branch of mathematics can be developed formally on the basis of axiomatic set theory. There are pitfalls, for example, having to do with large collections: for example, just as it is inconsistent to postulate the existence of a set of all sets, in the same way, there is no collection of all partial orders, or all groups. So when interpreting some mathematical claims, care has to be taken in some cases to restrict to sufficiently large collections of such objects. But this rarely amounts to more than careful bookkeeping, and it is a remarkable fact that, for the most part, the axioms of set theory are flexible and powerful enough to justify most ordinary mathematical constructions.

23.3.剩余的公理

23.3. The Remaining Axioms

我们已经看到的七条公理非常强大,足以代表大部分数学。我们在这里讨论 Zermelo-Fraenkel 集合论的其余公理。

The seven axioms we have seen are quite powerful, and suffice to represent large portions of mathematics. We discuss the remaining axioms of Zermelo-Fraenkel set theory here.

到目前为止,我们所见过的所有公理都没有排除集合x可以是它自身的一个元素,也就是说,我们可以得到xx。下面的公理排除了这一点。

So far, none of the axioms we have seen rule out the possibility that a set x can be an element of itself, that is, that we can have xx. The following axiom precludes that.

Foundationx(yyxyxzxzy)))

这条公理说,如果x是非空集,有一个元素yx具有以下属性:y再次成为x。这意味着我们不能有一个下降的集合链,每个集合都是前一个集合的元素:

The axiom says that if x is a nonempty set, there is an element y of x with the property that no element of y is again an element of x. This implies we cannot have a descending chain of sets, each one an element of the one before:

x1x2x3

如果我们将基础公理应用于集合{x1,x2,x3,},我们发现一些元素xi不包含任何其他内容,这只有在序列以以下方式终止时才有可能xi换句话说,该公理蕴含(并且实际上等同于)元素关系是建立良好的陈述,这解释了该名称。

If we apply the axiom of foundation to the set {x1,x2,x3,}, we find that some element xi does not contain any others, which is only possible if the sequence has terminated with xi. In other words, the axiom implies (and is in fact equivalent to) the statement that the elementhood relation is well founded, which explains the name.

上一节列出的公理讲述了集合是如何形成的:我们从空集开始,不断应用幂集、并集和分离等构造来构建更多集合。集合论者经常将集合的层次想象成一个大 V 字,空集位于最底层,任何更高级别的集合都包含出现在较低级别的集合作为其元素。从精确的意义上讲(我们不会在这里详细说明),基础公理表明每个集合都是以这种方式产生的。

The axioms listed in the previous section tell a story of how sets come to be: we start with the empty set, and keep applying constructions like power set, union, and separation, to build more sets. Set theorists often imagine the hierarchy of sets as forming a big V, with the empty set at the bottom and a set at any higher level comprising, as its elements, sets that appear in levels below. In a precise sense (which we will not spell out here), the axiom of foundation says that every set arises in such a way.

现在考虑以下集合序列:

Now consider the following sequence of sets:

N,P(N),P(P(N),P(P(P(N))),

这与我们迄今为止看到的所有公理一致,即数学宇宙中的每个集合都是其中一个元素。这仍然给我们很多集合,但是,既然我们已经描述了该序列,我们也可以想象一个包含所有集合的集合:

It is consistent with all the axioms we have seen so far that every set in the mathematical universe is an element of one of these. That still gives us a lot of sets, but, since we have described that sequence, we can just as well imagine a set that contains all of them:

{N,P(N),P(P(N),P(P(P(N))),}.

下列公理暗示了这样一个集合的存在。

The following axiom implies the existence of such a set.

Replacement:x,y1,,yn(zx!wA(z,w,y1,,yn)uw(wuzxA(z,w,y1,,yn)))

和分离公理一样,这个公理实际上是一个模式,也就是说,每个公式都有一个单独的公理A. 这里,变量y1,y2,,yn是可以出现在A。要理解这个公理,最简单的方法是将它们视为背景中固定的参数,然后忽略它们。公理说,如果对于每个zx有一个独特的w令人满意A(z,w),则有一个集合,u,包括w对应于每一个这样的值z换句话说,如果你想A作为一个函数,其定义域为x,公理断言该函数的范围存在。在上面的例子中,x是自然数,并且A(z,w)wz-自然数幂集的倍迭代。

Like the axiom of separation, this axiom is really a schema, which is to say, a separate axiom for each formula A. Here, too, the variables y1,y2,,yn are free variables that can occur in A. To understand the axiom, it is easiest to think of them as parameters that are fixed in the background, and then ignore them. The axioms says that if, for every z in x there is a unique w satisfying A(z,w), then there is a single set, u, that consists of the w values corresponding to every such z. In other words, if you think of A as a function whose domain is x, the axiom asserts that the range of that function exists. In the example above, x is the natural numbers, and A(z,w) says that w is the z-fold iterate of the power set of the natural numbers.

到目前为止,我们列出的九条公理构成了所谓的Zermelo-Fraenkel 集合论。还有一个附加公理,即选择公理,由于历史原因,它通常被单独列出:它曾被认为是有争议的,在早期,数学家认为跟踪公理是否在证明中实际使用很重要。有许多等效的表述,但这个是最直接的表述之一。

The nine axioms we have listed so far comprise what is known as Zermelo-Fraenkel Set Theory. There is on additional axiom, the axiom of choice, which is usually listed separately for historical reasons: it was once considered controversial, and in the early days, mathematicians considered it important to keep track of whether the axiom was actually used in a proof. There are many equivalent formulations, but this one is one of the most straightforward.

Choice:x(xf:xxyxf(y)y)

公理说,对于任何集合x非空集,有一个函数f从每个元素中选择一个元素。我们在第 15.2 节中非正式地使用了这个公理来证明每个全射函数都有一个右逆。事实上,根据其他公理,可以证明最后一个陈述等同于选择公理。

The axiom says that for any collection x of nonempty sets, there is a function f that selects an element from each one. We used this axiom, informally, in Section 15.2 to show that every surjective function has a right inverse. In fact, this last statement can be shown to be equivalent to the axiom of choice on the basis of the other axioms.

总结一下,策梅洛-弗兰克尔集合论的公理和选择公理如下:

To summarize, then, the axioms of Zermelo-Fraenkel Set Theory with the axiom of choice are as follows:

  1. 外延性:

    x,y(x=yz(zxzy))
  2. Extensionality:

    x,y(x=yz(zxzy))
  3. 空集:

    xyyx
  4. Empty set:

    xyyx
  5. 搭配:

    x,yzw(wzw=xw=y)
  6. Pairing:

    x,yzw(wzw=xw=y)
  7. 联盟:

    xyz(zyw(wxzw))
  8. Union:

    xyz(zyw(wxzw))
  9. 功率设置:

    xyz(zyzy)
  10. Power set:

    xyz(zyzy)
  11. 分离:

    x1,x2,,xn,yzw(wzwyA(w,x1,x2,,xn))
  12. Separation:

    x1,x2,,xn,yzw(wzwyA(w,x1,x2,,xn))
  13. 无穷大:

    x(xy(yxy{y}x))
  14. Infinity:

    x(xy(yxy{y}x))
  15. 基础:

    x(yyxyxzxzy)))
  16. Foundation:

    x(yyxyxzxzy)))
  17. 替代品:

    x,y1,,yn(zx!wA(z,w,y1,,yn)uw(wuzxA(z,w,y1,,yn)))
  18. Replacement:

    x,y1,,yn(zx!wA(z,w,y1,,yn)uw(wuzxA(z,w,y1,,yn)))
  19. 选择:

    x(xf:xxyxf(y)y)
  20. Choice:

    x(xf:xxyxf(y)y)

23.4.类型理论

23.4. Type Theory

作为数学的基础,策梅洛-弗兰克尔集合论颇具吸引力。其底层逻辑——一阶逻辑,为量词和逻辑联结词提供了基本的逻辑框架。除此之外,该理论还描述了一个单一的、直观自然的概念,即一组元素。这些公理非常合理。几乎所有现代数学都可以简化为如此简单的术语,这真是令人惊叹。

As a foundation for mathematics, Zermelo-Fraenkel set theory is appealing. The underlying logic, first-order logic, provides the basic logical framework for quantifiers and the logical connectives. On top of that, the theory describes a single, intuitively natural concept, that of a set of elements. The axioms are plausible eminently reasonable. It is remarkable that virtually all of modern mathematics can be reduced to such simple terms.

然而,还有其他基础可供选择。这些基础往往在很大程度上可以与集合论相互解释。毕竟,集合论语言现在在日常数学中无处不在,所以任何合理的基础都应该能够理解这种语言。另一方面,我们已经注意到集合论具有极强的表达能力和鲁棒性,因此其他基础方法通常可以用集合论术语来理解也就不足为奇了。

There are other foundations on offer, however. These tend to be largely inter-interpretable with set theory. After all, set-theoretic language is now ubiquitous in everyday mathematics, so any reasonable foundation should be able to make sense of such language. On the other hand, we have already noted that set theory is remarkably expressive and robust, and so it should not be surprising that other foundational approaches can often be understood in set-theoretic terms.

依赖类型理论尤其如此,它是 Lean 定理证明器的基础。类型理论的语法比集合论的语法更复杂。在集合论中,只有一种对象;正式地说,一切都是集合。相比之下,在类型理论中,Lean 中每个格式正确的表达式都有一个类型,并且有丰富的定义类型的词汇表。

This is, in particular, true of dependent type theory, which is the basis of the Lean theorem prover. The syntax of type theory is more complicated than that of set theory. In set theory, there is only one kind of object; officially, everything is a set. In contrast, in type theory, every well-formed expression in Lean has a type, and there is a rich vocabulary of defining types.

事实上,精益是基于一种被称为归纳构造演算的公理框架版本,它提供了以下所有内容:

In fact, Lean is based on a version of an axiomatic framework known as the Calculus of Inductive Constructions, which provides all of the following:

  • 类型宇宙的层次结构,,,,,… 以及特殊类型。表达式缩写,并且说可以解释为说是一种数据类型。类型是命题的类型。Type 0Type 1Type 2PropTypeType 0T : TypeTProp

  • A hierarchy of type universes, Type 0, Type 1, Type 2, … and a special type Prop. The expression Type abbreviates Type 0, and saying T : Type can be interpreted as saying that T is a datatype. The type Prop is the type of propositions.

  • 依赖函数类型 。此类型的元素是将任何类型元素映射到类型元素的函数。输出类型取决于输入类型,这就是函数“依赖”的原因。在输出类型不依赖于输入的情况下,我们有简单的函数类型。Π x : A, B xfaAf aB aA B

  • Dependent function types Π x : A, B x. An element f of this type is a function which maps any element a of type A to an element f a of type B a. The fact that the type of the output depends on the type of the input is what makes the function “dependent.” In the case where the output type does not depend on the input, we have the simple function type A B.

  • 归纳类型,如自然数,由构造函数指定,如零和后继。每种此类类型都带有归纳和递归原理。

  • Inductive types, like the natural numbers, specified by constructors, like zero and successor. Each such type comes with principles of induction and recursion.

这些构造既解释了断言(即命题)的底层逻辑,也解释了宇宙的对象(普通类型的元素)。

These constructions account for both the underlying logic of assertions (that is, the propositions) as well as the objects of the universe, which are elements of the ordinary types.

在集合论中解释类型论很简单,因为我们可以将每种类型视为一个集合。类型宇宙只是集合的大集合,依赖函数类型和归纳类型可以用集合论构造来解释。我们可以将集合Prop视为{,}真值,就像我们描述命题逻辑的真值表语义时所做的那样。

It is straightforward to interpret type theory in set theory, since we can view each type as a set. The type universes are simply large collections of sets, and dependent function types and inductive types can be explained in terms of set-theoretic constructions. We can view Prop as the set {,} of truth values, just as we did when we described truth-table semantics for propositional logic.

考虑到最后一个事实,为什么不直接使用集合论而不是类型论来进行交互式定理证明呢?一些交互式定理证明器就是这样做的。但类型论有一些优势:

Given this last fact, why not just use set theory instead of type theory for interactive theorem proving? Some interactive theorem provers do just that. But type theory has some advantages:

  • 表达式的形成规则如此严格,使得系统更容易识别印刷错误并提供有用的反馈。在类型论中,如果f具有类型,则只能应用于自然数,并且如果参数的类型错误,定理证明器可以标记错误。在集合论中,任何东西都可以应用于任何东西,无论这样做是否真的有意义。

  • The fact that the rules for forming expressions are so rigid makes it easier for the system to recognize typographical errors and provide useful feedback. In type theory, if f has type it can be applied only to a natural number, and a theorem prover can flag an error if the argument has the wrong type. In set theory, anything can be applied to anything, whether or not doing so really makes sense.

  • 同样,由于形成表达式的规则非常严格,系统可以从表达式的组成部分推断出有用的信息,而集合论则要求我们将这些信息明确化。例如,如上所述,定理证明器可以推断出中的f变量应该具有类型,并且结果表达式再次具有类型。在集合论中,xf xxN必须以明确的假设来表述,并且f(x)N那么就是一个定理。

  • Again, because the rules for forming expressions are so rigid, the system can infer useful information from the components of an expression, whereas set theory would require us to make such information explicit. For example, with f as above, a theorem prover can infer that a variable x in f x should have type , and that the resulting expression again has type . In set theory, xN has to be stated as an explicit hypothesis, and f(x)N is then a theorem.

  • 通过将命题编码为某些类型,我们可以使用相同的语言来定义数学对象和编写数学证明。例如,我们可以将函数应用于参数,就像我们将定理应用于某些假设一样。

  • By encoding propositions as certain kinds of types, we can use the same language for defining mathematical objects and writing mathematical proofs. For example, we can apply a function to an argument in the same way we apply a theorem to some hypotheses.

  • 依赖类型理论中足够纯粹的部分中的表达式具有计算解释,因此,例如,逻辑框架告诉我们如何在给定其定义的情况下评估阶乘函数。在集合论中,计算解释是在事后独立指定的。

  • Expressions in a sufficiently pure part of dependent type theory have a computational interpretation, so, for example, the logical framework tells us how to evaluate the factorial function, given its definition. In set theory, the computational interpretation is specified independently, after the fact.

这些事实让我们回想起我们在第 1 章中提出的关注点分离:不同的公理基础提供不同的理想化数学活动描述,并可设计用于不同的目的。如果您想要一个干净、简单的理论来解释绝大多数数学证明,集合论是您的不二之选。如果您正在寻找一个以计算为中心或以函数而不是集合的概念为基础的基础,各种类型论都有其魅力。对于交互式定理证明,关于实现和可用性的实际问题就会发挥作用。重要的是要认识到,所有这些理想化的描述的共同点是,它们都是为模拟数学语言和证明的重要方面而设计的。我们的目标是帮助您反思数学语言和证明的那些赋予数学特殊性质的特征,并帮助您更好地理解它们的工作原理。

These facts hark back to the separation of concerns that we raised in Chapter 1: different axiomatic foundations provide different idealized descriptions of mathematical activity, and can be designed to serve different purposes. If you want a clean, simple theory that accounts for the vast majority of mathematical proof, set theory is hard to beat. If you are looking for a foundation that makes computation central or takes the notion of a function rather than a set as basic, various flavors of type theory have their charms. For interactive theorem proving, pragmatic issues regarding implementation and usability come into play. What is important to recognize is that what all these idealized descriptions have in common is that they are all designed to model important aspects of mathematical language and proof. Our goal here has been to help you reflect on those features of mathematical language and proof that give mathematics its special character, and to help you better understand how they work.

23.5.练习

23.5. Exercises

  1. 使用类似罗素悖论的论点来表明不存在“所有集合的集合”,也就是说,不存在包含其他所有集合作为元素的集合。

  2. Use an argument similar Russell’s paradox to show that there is no “set of all sets,” that is, there is no set that contains every other set as an element.

  3. 认为x是一个非空集合,包含一个元素y. 利用分离公理证明集合x存在。(记住某物是x如果它是x

  4. Suppose x is a nonempty set, say, containing an element y. Use the axiom of separation to show that the set x exists. (Remember that something is an element of x if it is an element of every element of x.)

  5. 证明第 23.1 节中的主张,即x×y是...的一个元素P(P(xy))

  6. Justify the claim in Section 23.1 that every element of x×y is an element of P(P(xy)).

  7. 给定一个集合x和一个函数A:xy,利用集合论的公理来证明ixA(i)

  8. Given a set x and a function A:xy, use the axioms of set theory to prove the existence of ixA(i).

24.附录:自然演绎规则

24. Appendix: Natural Deduction Rules

含义:

Implication:

连词:

Conjunction:

否定:

Negation:

析取:

Disjunction:

真与假:

Truth and falsity:

双重含义:

Bi-implication:

归谬法(反证法):

Reductio ad absurdum (proof by contradiction):

全称量词:

The universal quantifier:

在介绍规则中,x在任何未取消的假设中都不应该自由。在消除规则中,t可以是任何不与任何绑定变量冲突的术语A

In the introduction rule, x should not be free in any uncanceled hypothesis. In the elimination rule, t can be any term that does not clash with any of the bound variables in A.

存在量词:

The existential quantifier:

在介绍规则中,t可以是任何不与任何绑定变量冲突的术语A在淘汰规则中,y不应该免费B或任何未被取消的假设。

In the introduction rule, t can be any term that does not clash with any of the bound variables in A. In the elimination rule, y should not be free in B or any uncanceled hypothesis.

平等:

Equality:

严格来说,只有refl和第二条替换规则是必需的。其他规则可以从它们中推导出来。

Strictly speaking, only refl and the second substitution rule are necessary. The others can be derived from them.